B-and-P B-and-P - 1 year ago 123
Bash Question

Shell script not running via crontab on Linux Mint

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

EDITION="Cinnamon 64-bit"
DESCRIPTION="Linux Mint 17.3 Rosa"
GRUB_TITLE=Linux Mint 17.3 Cinnamon 64-bit

Custom shell script




# 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
), it outputs a file
with output of
in it.

Now, I have setup a cron job via
crontab -e

# create text files every minute:
* * * * * 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
, 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

are not recognized?

Answer Source

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 list.


* * * * * 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

export PATH=$PATH:/bin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin

(just before using the commands).

P.S.: use which command in a terminal to detect location of a command, e.g. which ls.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download