2

In my crontab I want to do something like:

# crontab
PYTHONPATH=$HOME/python_proj_dir/
SC=$PYTHONPATH/script_dir/ # SC = ShortCut
0 * * * * python27 $SC/script.py

However only doing:

PYTHONPATH=/home/me/python_proj_dir/

works. The line running the python script won't even work if SC=/home/me/python_proj_dir/script_dir/.

I also tried first setting HOME:

HOME=/home/me/
0 * * * * python27 $HOME/script.py # This does not work

The errors I got were similar to:

python27: can't open file '$HOME/script.py': [Errno 2] No such file or directory

I have gotten it to work by using full/absolute paths, but I want to use a variable $SC (shortcut) so that the lines aren't over 9000 characters long, and so that it is easy to move the project to, for example another users dir or a new company server by simply changing $SC.

I saw this, but it was for sending a variable to a program.

Community
  • 1
  • 1
ZN13
  • 1,048
  • 1
  • 11
  • 21
  • `$HOME` is something cron user doesn't know about. Also, I doubt `python27` alone works: you should be using the full path of this binary. – fedorqui Jun 21 '16 at 11:26
  • Ah, should this have been posted to Unix & Linux instead, maybe? – ZN13 Jun 21 '16 at 11:26
  • @fedorqui I got python27 working by adding my path. PATH=....stuff.... – ZN13 Jun 21 '16 at 11:27
  • Ah right, I missed that part. The problem here is that you cannot set a variable based on another one. See [Linux environment variables referencing other variables](http://stackoverflow.com/a/37940991/1983854). I just tested it with the following cron: `SC="hello"` then `PY=$SC/ba` and finally `* * * * * env >> /tmp/myenv`. In `myenv` I get `PY=$SC/ba`, tha tis, `$SC` does not get substituted. – fedorqui Jun 21 '16 at 11:31
  • @fedorqui I see... That must be why $HOME/script.py isn't working either. Is there a way to make it work? Concatenate a variable with a path-string? – ZN13 Jun 21 '16 at 11:36
  • Just change the logic and remove all the complicated things from crontab and move them to the script itself. – fedorqui Jun 21 '16 at 11:40

0 Answers0