What is the correct syntax to reference shared variable in a start command?
trevin
HOBBYOP

a month ago

I'm trying to have a service act as a cron job with a scheduling every N minutes calling an endpoint of mine. But this isn't working:

curl -X POST https://app.railway.internal/some-api/endpoint -H "x-cron-secret: $CRON_SECRET" -f

I'm not sure if the syntax on the secret is incorrect OR if it's the entire start command that isn't correct.

I'm not 100% sure it's the variable or not, as there are no logs for this type of usage in railway. Would appreciate help!

Solved$10 Bounty

Pinned Solution

brody
EMPLOYEE

a month ago

No loop needed.

Just wrap the command in a shell as outlined in the docs -

https://docs.railway.com/reference/build-and-start-commands#start-command

Your cron service will also need it's source set to an image that has the curl binary built in.

7 Replies

In the variables table in your service configuration, add a variable that references the shared variable. (Eg, ${{ shared.SOME_VARIABLE }} as shown in image)Btw, there are documentations on how to use shared variables. https://docs.railway.com/guides/variables#use-a-shared-variable

Attachments


0x5b62656e5d

In the variables table in your service configuration, add a variable that references the shared variable. (Eg, ${{ shared.SOME_VARIABLE }} as shown in image)Btw, there are documentations on how to use shared variables. https://docs.railway.com/guides/variables#use-a-shared-variable

trevin
HOBBYOP

a month ago

Ok i've tired that, but it doesn't seem to be working still...

is the command correct then as the start command like this:

curl -X POST https://app.railway.internal/some-api/endpoint -H "x-cron-secret: $CRON_SECRET" -f

? it doesn't seem to successfully run. I then just tried putting my cron secret in place instead of referencing the variable and IT STILL DIDN'T work. So it's not the variable expansion it's something else.

is it just not possible to run curl commands like this as the start command?


trevin

Ok i've tired that, but it doesn't seem to be working still...is the command correct then as the start command like this:curl -X POST https://app.railway.internal/some-api/endpoint -H "x-cron-secret: $CRON_SECRET" -f? it doesn't seem to successfully run. I then just tried putting my cron secret in place instead of referencing the variable and IT STILL DIDN'T work. So it's not the variable expansion it's something else.is it just not possible to run curl commands like this as the start command?

zuneec
HOBBY

a month ago

The variable syntax is correct:

  • In the Variables table: CRON_SECRET = ${{ shared.CRON_SECRET }}

  • In the start command: $CRON_SECRET

Yes, you can run curl as a start command, but it needs to be a long-running process.

The problem isn't that curl commands are forbidden—it's that curl executes once and exits. When a start command's process exits, Railway considers the service stopped/crashed.

Possible fix:

Wrap it in a loop:

while true; do curl -X POST http://app.railway.internal/some-api/endpoint -H "x-cron-secret: $CRON_SECRET" -f; sleep 300; done

Or with sh -c if needed:

sh -c 'while true; do curl -X POST http://app.railway.internal/some-api/endpoint -H "x-cron-secret: $CRON_SECRET" -f; sleep 300; done'

And try to change https:// to http:// for internal networking.

but there's a trade-off to this solution, it wastes resrouces... It keeps a container running 24/7 just to execute a curl command every few minutes—wasteful and costs money unnecessarily.

It's better to use Railway's Cron Service in the service settings that calls the endpoint on a schedule.

https://docs.railway.com/reference/cron-jobs

https://docs.railway.com/guides/cron-jobs


brody
EMPLOYEE

a month ago

No loop needed.

Just wrap the command in a shell as outlined in the docs -

https://docs.railway.com/reference/build-and-start-commands#start-command

Your cron service will also need it's source set to an image that has the curl binary built in.


zuneec

The variable syntax is correct:In the Variables table: CRON_SECRET = ${{ shared.CRON_SECRET }}In the start command: $CRON_SECRETYes, you can run curl as a start command, but it needs to be a long-running process.The problem isn't that curl commands are forbidden—it's that curl executes once and exits. When a start command's process exits, Railway considers the service stopped/crashed.Possible fix:Wrap it in a loop:while true; do curl -X POST http://app.railway.internal/some-api/endpoint -H "x-cron-secret: $CRON_SECRET" -f; sleep 300; doneOr with sh -c if needed:sh -c 'while true; do curl -X POST http://app.railway.internal/some-api/endpoint -H "x-cron-secret: $CRON_SECRET" -f; sleep 300; done'And try to change https:// to http:// for internal networking.but there's a trade-off to this solution, it wastes resrouces... It keeps a container running 24/7 just to execute a curl command every few minutes—wasteful and costs money unnecessarily.It's better to use Railway's Cron Service in the service settings that calls the endpoint on a schedule.https://docs.railway.com/reference/cron-jobshttps://docs.railway.com/guides/cron-jobs

trevin
HOBBYOP

a month ago

You said:

----

“...but there's a trade-off to this solution, it wastes resrouces... It keeps a container running 24/7 just to execute a curl command every few minutes—wasteful and costs money unnecessarily.

It's better to use Railway's Cron Service in the service settings that calls the endpoint on a schedule.”

----

I’m a bit confused. The service instance setup is setup to use cron in the setting of the service to run every 5 mins.

It’s just the custom start command I’m having trouble with.

What’s wasted resources are you talking about? Or did you think i was leaving the service on all the time for some reason?


brody

No loop needed.Just wrap the command in a shell as outlined in the docs -https://docs.railway.com/reference/build-and-start-commands#start-commandYour cron service will also need it's source set to an image that has the curl binary built in.

trevin
HOBBYOP

a month ago

Ok my stupidity here is i didn't realize I needed to specify an image. I had incorrectly assumed there was always some default like alpine linux etc.

I set it to use curlimages/curl and now have the service running at least and am seeing logs. I'm getting this error in the logs:

curl: (7) Failed to connect to app.railway.internal port 80 after 20 ms: Could not connect to server

i tried it with SSL and getting:

curl: (7) Failed to connect to app.railway.internal port 443 after 20 ms: Could not connect to server

trevin
HOBBYOP

a month ago

Ok I got it! the missing link was the port number (duh).

So for anyone else looking at this in the future for how to get a lightweight cron job going on railway:

  1. Use the curlimages/curl docker image

  2. Make sure the CRON_SECRET variable is defined in the service.

  3. the command I used was this (my port on my app service is 8080)/bin/sh -c 'exec curl -fsS -X POST http://my-app.railway.internal:8080/api/endpoint -H "x-cron-secret: $CRON_SECRET"'

Then setup the cron schedule in the service.


Status changed to Open brody 27 days ago


Status changed to Solved brody 27 days ago


Loading...