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.
System Info
RELEASE=17.3
CODENAME=rosa
EDITION="Cinnamon 64-bit"
DESCRIPTION="Linux Mint 17.3 Rosa"
DESKTOP=Gnome
TOOLKIT=GTK
NEW_FEATURES_URL=http://www.linuxmint.com/rel_rosa_cinnamon_whatsnew.php
RELEASE_NOTES_URL=http://www.linuxmint.com/rel_rosa_cinnamon.php
USER_GUIDE_URL=help:linuxmint
GRUB_TITLE=Linux Mint 17.3 Cinnamon 64-bit
Custom shell script
Location: /usr/local/bin/make_ls_files.sh
Contents:
#!/bin/bash
# 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
The script runs fine from terminal (from any cwd
), it outputs a file /home/zion/ls_file_2016-11-17_17-38-37.log
with output of ls
in it.
Now, I have setup a cron job via crontab -e
:
# create text files every minute:
PATH=/usr:/usr/bin:/usr/local/bin
* * * * * make_ls_files.sh >> /usr/local/bin/make_ls_files_log.log 2>&1
I see in the syslog that it is in fact running, but no output or log entry is created. syslog shows this:
...
Nov 17 17:42:01 zion-VirtualBox CRON[4845]: (zion) CMD (make_ls_files.sh >> /usr/local/bin/make_ls_files_log.log 2>&1)
Nov 17 17:42:01 zion-VirtualBox CRON[4844]: (CRON) info (No MTA installed, discarding output)
Nov 17 17:43:02 zion-VirtualBox CRON[4850]: (zion) CMD (make_ls_files.sh >> /usr/local/bin/make_ls_files_log.log 2>&1)
Nov 17 17:43:02 zion-VirtualBox CRON[4849]: (CRON) info (No MTA installed, discarding output)
Any ideas as to why it isn't working and how to debug?
EDIT 01:
I changed the cron job to:
* * * * * make_ls_files.sh 2>>$HOME/crontab.log
Now I am getting somewhere, the log file is created in /home/zion/crontab.log
, containing:
/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
Why ls
and date
are not recognized?
EDIT 02
Thanks to all, I learned a few things here. As Ruslan pointend out I had 2 issues:
- insufficient permission for log file location
- insufficient path definitions
Everything is working now, the final cronjob below:
# create text files every minute:
PATH=/bin:/usr:/usr/bin:/usr/local/bin
# * * * * * make_ls_files.sh >> /usr/local/bin/make_ls_files_log.log 2>&1
* * * * * make_ls_files.sh 2 >> $HOME/crontab.log