How to get CRON to call in the correct PATHs
I used /etc/crontab
. I used vi
and entered in the PATHs I needed into this file and ran it as root. The normal crontab overwrites PATHs that you have set up. A good tutorial on how to do this.
The systemwide cron file looks like this:
This has the username field, as used by /etc/crontab.
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file.
# This file also has a username field, that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
42 6 * * * root run-parts --report /etc/cron.daily
47 6 * * 7 root run-parts --report /etc/cron.weekly
52 6 1 * * root run-parts --report /etc/cron.monthly
01 01 * * 1-5 root python /path/to/file.py
Where is user's cron job stored after crontab -e?
It is stored in the directory:
/var/spool/cron/crontabs
Containing one file per user.
From man crontab
(at least on my Ubuntu 13):
There is one file for each user's crontab under the
/var/spool/cron/crontabs directory. Users are not allowed to edit the
files under that directory directly to ensure that only users allowed
by the system to run periodic tasks can add them, and only
syntactically correct crontabs will be written there. This is
enforced by having the directory writable only by the crontab group
and configuring crontab command with the setgid bid set for that
specific group.
how to set path for crontab in ubuntu
Always use absolute paths when adding cronjobs:
24 20 09 11 * /usr/bin/python3 /bin/remote_iomodified.py
Where can I set environment variables that crontab will use?
Have 'cron' run a shell script that sets the environment before running the command.
Always.
# @(#)$Id: crontab,v 4.2 2007/09/17 02:41:00 jleffler Exp $
# Crontab file for Home Directory for Jonathan Leffler (JL)
#-----------------------------------------------------------------------------
#Min Hour Day Month Weekday Command
#-----------------------------------------------------------------------------
0 * * * * /usr/bin/ksh /work1/jleffler/bin/Cron/hourly
1 1 * * * /usr/bin/ksh /work1/jleffler/bin/Cron/daily
23 1 * * 1-5 /usr/bin/ksh /work1/jleffler/bin/Cron/weekday
2 3 * * 0 /usr/bin/ksh /work1/jleffler/bin/Cron/weekly
21 3 1 * * /usr/bin/ksh /work1/jleffler/bin/Cron/monthly
The scripts in ~/bin/Cron are all links to a single script, 'runcron', which looks like:
: "$Id: runcron.sh,v 2.1 2001/02/27 00:53:22 jleffler Exp $"
#
# Commands to be performed by Cron (no debugging options)
# Set environment -- not done by cron (usually switches HOME)
. $HOME/.cronfile
base=`basename $0`
cmd=${REAL_HOME:-/real/home}/bin/$base
if [ ! -x $cmd ]
then cmd=${HOME}/bin/$base
fi
exec $cmd ${@:+"$@"}
(Written using an older coding standard - nowadays, I'd use a shebang '#!' at the start.)
The '~/.cronfile' is a variation on my profile for use by cron - rigorously non-interactive and no echoing for the sake of being noisy. You could arrange to execute the .profile and so on instead. (The REAL_HOME stuff is an artefact of my environment - you can pretend it is the same as $HOME.)
So, this code reads the appropriate environment and then executes the non-Cron version of the command from my home directory. So, for example, my 'weekday' command looks like:
: "@(#)$Id: weekday.sh,v 1.10 2007/09/17 02:42:03 jleffler Exp $"
#
# Commands to be done each weekday
# Update ICSCOPE
n.updics
The 'daily' command is simpler:
: "@(#)$Id: daily.sh,v 1.5 1997/06/02 22:04:21 johnl Exp $"
#
# Commands to be done daily
# Nothing -- most things are done on weekdays only
exit 0
Using Relative Paths in Crontab
After many trials and research, I discovered that the solution was using the HOME=
variable, not the PATH=
variable, like so:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=""
HOME=/var/www/html/crons
And then each of the lines would just look like:
*/2 * * * * root /usr/bin/php cronfile.php >> logs/cronfile_`date +\%Y\%m\%d`.log
Hope this helps someone else with the same issue I had in the future.
Related Topics
Linux: Find File Names With 4 or 5 Characters
How to Convert Hex to Ascii Characters in the Linux Shell
What Happens If You Use the 32-Bit Int 0X80 Linux Abi in 64-Bit Code
Multiple Glibc Libraries on a Single Host
Read Values into a Shell Variable from a Pipe
How to Link to a Specific Glibc Version
Understanding Linux /Proc/Pid/Maps or /Proc/Self/Maps
How to Keep Environment Variables When Using Sudo
How to Store a Command in a Variable in a Shell Script
How to Grep For Contents After Pattern
Deploying Yesod to Heroku, Can't Build Statically
How to Kill a Child Process After a Given Timeout in Bash
What's the Difference of Section and Segment in Elf File Format
How to Compare Two Strings in Dot Separated Version Format in Bash
How to Split CSV Files as Per Number of Rows Specified
How Can a Process Intercept Stdout and Stderr of Another Process on Linux