2 months 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" -fI'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!
Pinned Solution
2 months 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
2 months ago
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
2 months 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?
2 months 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; 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.
2 months 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
2 months 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.
2 months 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 serveri tried it with SSL and getting:
curl: (7) Failed to connect to app.railway.internal port 443 after 20 ms: Could not connect to server2 months 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:
Use the
curlimages/curldocker imageMake sure the CRON_SECRET variable is defined in the service.
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 • about 2 months ago
Status changed to Solved brody • about 2 months ago
