I am running a python script using a cron job. When the script runs, it either alerts that it completed or failed. It is supposed to send a message to cronitor along with the completion ping. An example of the link is below:
https://cronitor.link/p/id/key?state=complete&msg='Success'
When I just put the link into the search bar and hit Enter, the message shows up in cronitor. However, when I try to get the message to print when running the link through the script it doesn’t work. Cronitor gets the ping that it completed successfully but no message shows up:
cron_alert = "/usr/local/bin/curl --silent https://cronitor.link/p/id/key?state=complete&msg='Success'" os.system(cron_alert)
I tried removing ‘–silent’ but that didn’t make a difference. Any ideas on how to fix this? Thanks
Advertisement
Answer
Your command contains an unescaped &
, which the shell used by os.system
parses as a command terminator that runs curl
the the background. You need to escape it, e.g.
cron_alert = "/usr/local/bin/curl --silent "https://cronitor.link/p/id/key?state=complete&msg='Success'""
but even better would be to stop using os.system
and use subprocess.run
instead.
import subprocess subprocess.run(["/usr/local/bin/curl", "--silent", "https://cronitor.link/p/id/key?state=complete&msg='Success'"])
which bypasses the shell altogether.
Best, use a library like requests
to send the request from Python, rather than forking a new process.
import requests requests.get("https://cronitor.link/p/id/key", params={'state': 'complete', 'msg': "'Success'"})
(In all cases, does the API really require single quotes around Success
?)