'Crontab in Raspberry pi doesn't run a very simple script

I'm new to Linux and I've been struggling with this issue for a while in my Raspberry Pi and had no success.

First I wrote a simple script in /home/myfile.sh like this:

#!/bin/bash
clear
echo "hi"

Then I did the sudo chmod 755 /home/myfile.sh to grant the permissions.

And finally I modified the crontab using crontab -e:

# some comments ...
* * * * * /home/myfile.sh

The problem:

When I run the script manually it works fine but when I set the above line in my crontab, nothing ever happens. What am I doing wrong?



Solution 1:[1]

In order to show how I managed to solve my issue with the hope of helping others, Here I post it as an answer to my own question:

I got around the problem by using system-wide crontab (/etc/crontab) instead of per user crontab (crontab -e).

To clarify this,

/etc/crontab is the system-wide crontab:

# m h dom mon dow user      command
*   *  *   *   *  someuser  echo 'foo'

while crontab -e is per user 'crontab':

# m h  dom mon dow  command
*   *   *   *   *   echo 'foo'

Notice in a per user crontab there is no 'user' field.

Solution 2:[2]

Try redirecting the output to a file like this :

* * * * * /home/myfile.sh > output_file.txt

and you will see it is working.

Cron jobs don't output to the same terminal you are using.


Edit

If you are using crontab -e for scheduling jobs, you are using a user's specific crontab. Thus, you can only write to that user's home directory (or other directories he has access to). So if you modify you cron job to:

* * * * * /home/myfile.sh > /home/<username>/output_file.txt

You will see that the output was written to that file under the user's home directory.

If you want to write to other directories, I suggest you use the system-wide crontab in /etc/crontab

By the way, you might want to enable logging for cron jobs in order to track problems. You need to edit the /etc/rsyslog.conf or /etc/rsyslog.d/50-default.conf file and make sure you have the following line uncommented or add it if it is missing:

cron.*                         /var/log/cron.log

Then restart rsyslog and cron:

sudo service rsyslog restart
sudo service cron restart

Now you see if your command was run by cron or not.

Solution 3:[3]

Cron jobs return stdout and stderr via email by default, so you need to check there for your test.

In the standard raspian distribution there isn't an email client/agent, so you need to install it with for example:

sudo aptitude install bsd-mailx

and than you will be able to check for local emails with the command mail.

Typically cron jobs won't return any output redirecting all of it to a log file.

Solution 4:[4]

You can crontab under user (crontab -e) by adding PATH into crontab file:

SHELL=/bin/sh

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
* * * * * /home/pi/command

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Sigi
Solution 4 colidyre