Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Os.system doesn't push message in cron alert to cronitor?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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",
              payload={'state': 'complete', 'msg': "'Success'"})

(In all cases, does the API really require single quotes around Success?)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading