Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 330
Bahman_Aries Avatar asked Jan 18 '16 14:01

Bahman_Aries


People also ask

Why crontab scripts are not working?

One of the most frequent causes for the crontab job not being correctly executed is that a cronjob does not run under the user's shell environment. Another reason can be – not specifying the absolute path of the commands used in the script.

Why is my crontab entry not running?

Check permissionsAnother user might have created the cron job and you may not be authorized to execute it. Note that the root must own jobs added as files in a /etc/cron. */ directory. That means that if you don't have root permissions, you might not be able to access the job.


1 Answers

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.

like image 186
Bahman_Aries Avatar answered Oct 04 '22 03:10

Bahman_Aries