I am running a python script using the following command:

0,30 * * * * python /home/pi/pythonscripts/script.py &

and I would like to pass a variable 'test' to the script in the crontab, something like so:

0,30 * * * * python /home/pi/pythonscripts/script.py & test='hello'

Does anybody in the raspberry pi community have any idea how to do this?

  • For starters: I believe that the ampersand goes to the end of the command (after parameters) and is also not necessary for scripts run via cron. That being said, commands run by cron should accept parameters as given in the crontab. – Ghanima Jan 6 '15 at 20:23
  • The ampersand is terminating the command line, so adding test='hello' is really adding a completely new command that would be executed immediately, not waiting for the script.py to finish first. – rob Jan 8 '15 at 13:26

The ampersand (for sending a process in the background) is not necessary for commands issued to cron via the crontab as processes are already forked. The given examples work fine without the &. Actually the first version shows working parameters as it invokes the python interpreter with the filename of the script to be executed as parameter.

0,30 * * * * python /home/pi/pythonscripts/script.py foobar


0,30 * * * * /home/pi/pythonscripts/script.py foobar

where script.py contains the first line #!/bin/python3 and has executable flags.

An example how to use and print the passed parameter (per question of the OP):

import sys 
for arg in sys.argv:

Parsing through the list argv that contains the parameters passed to the script. argv[0] contains the name (and path) of the script. argv[1] is the first parameter, and so on. For more serious handling of arguments it is advisable to use argparse (tutorial) instead of reinventing the wheel.

  • How would I print the text foobar in Python? – JBithell Jan 7 '15 at 20:34
  • 1
    For one thing: import sys and then use the sys.argv list, e.g. argv[0] is the name of the script, argv[1] is the first parameter, and so on. Easily parse through the list with something like: for arg in sys.argv: – Ghanima Jan 8 '15 at 8:14

First, you need to remove the & sign as mentioned above. Cron forks processes already.

There are several ways to pass parameter to your script. Here are some examples (but not limited to):

  1. Using sys.argv list. Pass it as an argument script.py parameter1
  2. Using Python getopt module to parse parameters. Similar to above, but with more possibilities to do things like script.py --param1=yes --param2=data
  3. Using os.environ dictionary. Pass it using environment variables PARAM1=yes script.py

Methods #1 and #2 are usually preferred.

  • Hello super old post... Can a cron service be used with argparse for optional parameters? – HenryHub Jun 17 '20 at 13:24
  • @HenryHub, I see no issues why not. But if you have a question, you better ask as a new question instead of commenting on the unrelated one. This way you get more visibility and answers. – nochkin Jun 17 '20 at 17:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.