5

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?

Bahman_Aries
  • 4,658
  • 6
  • 36
  • 60

4 Answers4

9

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.

Bahman_Aries
  • 4,658
  • 6
  • 36
  • 60
8

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.

Mehdi Yedes
  • 2,267
  • 2
  • 14
  • 14
  • I tried your solution but nothing was written in my txt file. what I did was create a txt file in my home directory and then modified my crontab to `* * * * * /home/myfile.sh > /home/myText.txt` – Bahman_Aries Jan 18 '16 at 15:11
  • My guess is that you can't write to a file under the `/home/` directory because only root user has access to write to the `/home` direcroty. So try changing the cron job line to `* * * * * /home/myfile.sh > /home//myText.txt` – Mehdi Yedes Jan 18 '16 at 15:22
  • When I opened the system-wide crontab, I realized, unlike the regular crontab (which was empty by default), here there are some pre-defined crontab lines that seems they have been working all along. So I inserted my crontab line there and it worked. I also changed the directory of both my sh and txt from home to etc. Thank you for your answer. – Bahman_Aries Jan 19 '16 at 07:27
  • Thanks to explain how to enable log, I saw in log file that cron runs at UTC hour and not at my country hour ! – bcag2 Oct 25 '20 at 18:00
1

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.

Sigi
  • 4,826
  • 1
  • 19
  • 23
  • I've seen [posts](http://stackoverflow.com/a/5398044/1648849) where you can use crontab to schedule running programs. does your answer mean it only meant to email the output? or I misunderstood? – Bahman_Aries Jan 18 '16 at 15:08
  • I mean that given your (correct) crontab, you will get the output containing "hi", in the form of a local email to the user who created it. No further action are required on your side, that's the standard behaviour of cron jobs. You can run programs in crontab, and their output is reported by email, if not redirected. – Sigi Jan 18 '16 at 15:40
  • however the job won't be able to interact (clear) the tty (terminal session) where you are connected to: the program get run in an independent batch session. – Sigi Jan 18 '16 at 15:47
  • tnx i'll check it out – Bahman_Aries Jan 18 '16 at 16:08
0

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
colidyre
  • 4,170
  • 12
  • 37
  • 53