I am attempting to install a new cron task on a server. I am able to run the process via command line, but cannot run it via cron. This is due to enviroment variables for PATH and PYTHONPATH not being set correctly when the task is run via cron. (I am actually having this trouble with all cron jobs on the server, but am focusing on one process in this question for simplicity's sake.)
Here is the cron entry for the process:
### procmon NLite ###
*/2 * * * * . ~/.bashrc; cd /var/networkip/nlite/proc_mon; . bashrc; cd bin; ./proc_mon.py > /dev/null 2>&1
Now, bashrc in the proc_mon directory contains:
BASEDIR=$PWD
KODIAK_ROOT=$BASEDIR/
BASEPATH=$KODIAK_ROOT
pathadd PYTHONPATH ${BASEDIR}/lib
So, when the crontask is run - PYTHONPATH should be reset, but this is not happening.
There are several other older servers successfully running these procs via cron.
Solutions attempted thus far:
- compared all bashrc or bash_profiles from a successfully running the cron task to the server failing to run crons
- logged environ variables to confirm this is the issue
Solutions I am not interested in:
- a cron task to set enviromental variables. There are several procs that need to run via cron and all have different PATH and PYTHONPATH variables relative to that specific script. Doing this could get very messy and is just not a good long term solution as I install the same procs and crons on future servers.
Further info:
- Linux distro for server failing cron: CentOS release 6.8
- Linux distro for server with cron success: Red Hat Enterprise Linux Server release 5.2 (Tikanga)
Since the crons is able to work on other servers, there must be a reasonable solution to the problem.
pathadd
? Are you depending on bash-isms? cron uses /bin/sh by default I believe, so you might have to at least set SHELL to /bin/bash in the crontab -man 5 crontab
.pathadd
is defined. I assumed it was a bashism. If I manually cd into the directory and. bashrc
, thenecho
environ variables, everything works fine. For good measure, I tried changingpathadd
to export and still no go for crontasks.set +o posix
to the start of a cron tasks fixes the issue! I still need a more global solution (disabling POSIX for cron?), but a step in the right direction...