I am just learning Linux, so please bear with me, as might have overlooked something obvious here. I am currently trying to setup a cron job to run a custom shell script. This script is running fine from the terminal but not via cron. Below are the details. I will appreciate any insights from the experienced users.
DESCRIPTION="Linux Mint 17.3 Rosa"
GRUB_TITLE=Linux Mint 17.3 Cinnamon 64-bit
# test script to echo ls stout to files
# (learning cron jobs)
# tilde path not working in cron, trying full path
# ls > ~/ls_file_$(date +%F_%H-%M-%S).log
ls > /home/zion/ls_file_$(date +%F_%H-%M-%S).log
# create text files every minute:
* * * * * make_ls_files.sh >> /usr/local/bin/make_ls_files_log.log 2>&1
Nov 17 17:42:01 zion-VirtualBox CRON: (zion) CMD (make_ls_files.sh >> /usr/local/bin/make_ls_files_log.log 2>&1)
Nov 17 17:42:01 zion-VirtualBox CRON: (CRON) info (No MTA installed, discarding output)
Nov 17 17:43:02 zion-VirtualBox CRON: (zion) CMD (make_ls_files.sh >> /usr/local/bin/make_ls_files_log.log 2>&1)
Nov 17 17:43:02 zion-VirtualBox CRON: (CRON) info (No MTA installed, discarding output)
* * * * * make_ls_files.sh 2>>$HOME/crontab.log
/usr/local/bin/make_ls_files.sh: line 6: date: command not found
/usr/local/bin/make_ls_files.sh: line 6: ls: command not found
Most likely you don't have enough permissions to write to
/usr/local/bin directory. It is easily verified by running
touch /usr/local/bin/myfile in a terminal.
You should redirect the errors to a log file in order to find out the exact reason.
Make sure that all executables in the script are available through
PATH, i.e. the directories into the
PATH=$PATH:/bin:/usr:/usr/bin:/usr/local/bin * * * * * make_ls_files.sh >> /tmp/make_ls_files_log.log 2>>$HOME/crontab.log
Note, you need at least write (w) and execution (x) permissions for the log file directories!
Also note, that your Cron implementation should support overriding
PATH environment variable via
crontab. Otherwise, you should specify absolute paths for all executables. You can also adjust
PATH in the shell script as follows
(just before using the commands).
which command in a terminal to detect location of a command, e.g.