Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a valid path with a tilde not expanding in this cron job? [duplicate]

Tags:

I had the following lines in my crontab:

PY=/home/schemelab/install/miniconda/bin/python
ST=~/prg/surgetrader

# SURGE TRADER

00  * * * * cd $ST/src/ ; $PY download.py; $PY scan.py --buy 1

And when it ran the error message in my email was:

X-Cron-Env: <GT=~/prg/gridtrader>
X-Cron-Env: <AGT=~/prg/adsactly-gridtrader>
X-Cron-Env: <PY=/home/schemelab/install/miniconda/bin/python>
X-Cron-Env: <ST=~/prg/surgetrader>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/schemelab>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=schemelab>
Date: Sun, 30 Jul 2017 09:50:02 -0400 (EDT)

/bin/sh: 1: cd: can't cd to ~/prg/surgetrader/src/
/home/schemelab/install/miniconda/bin/python: can't open file 'takeprofit.py': [Errno 2] No such file or directory

However, the path certainly does exist. I think that the tilde is not being expanded or something.

like image 455
Terrence Brannon Avatar asked Jul 30 '17 14:07

Terrence Brannon


People also ask

What is the use of * * * * * In cron?

* * * * * is a cron schedule expression wildcard, meaning your cron job should run every minute of every hour of every day of every month, each day of the week.

How do I change the path of crontab?

Crontab Variables The default path is set to PATH=/usr/bin:/bin . If the command you are executing is not present in the cron specified path, you can either use the absolute path to the command or change the cron $PATH variable. You can't implicitly append :$PATH as you would do with a regular script.

Why crontab scripts are not working?

One of the most frequent causes for the crontab job not being correctly executed is that a cronjob does not run under the user's shell environment. Another reason can be – not specifying the absolute path of the commands used in the script.

How do you know if Crond is running?

To check to see if the cron daemon is running, search the running processes with the ps command. The cron daemon's command will show up in the output as crond. The entry in this output for grep crond can be ignored but the other entry for crond can be seen running as root. This shows that the cron daemon is running.


1 Answers

Tilde ~ resolution is a bash feature. However your cronjob is not executed through Bash (You could do it explicitly if you want). However you can use $HOME to refer to the user home independently of the shell.

Refer to Bash reference manual for more info.

like image 137
Anubis Avatar answered Oct 01 '22 03:10

Anubis