B-and-P B-and-P - 22 days ago 14
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

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?

Answer

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.

Example

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

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.