How to execute shell script via crontab?

22

8

I have a notify.sh script that looks like:

notify-send "hi welcome"

My crontab notification for 2 PM:

0 14 * * * home/hacks/notify.sh

However, this doesn't work. What is the problem?

Aravind

Posted 2009-12-10T08:38:25.970

Reputation: 401

Answers

38

Your script is missing a #! line at the start, which is the magic interpreted by the kernel to say which command interpreter is to be used for the script.

Make it look like this:

#!/bin/sh
notify-send "hi welcome"

and make sure the script is executable:

ls -l home/hacks/notify.sh
chmod +x home/hacks/notify.sh
ls -l home/hacks/notify.sh

Also, since you're asking for this to happen just once a day, is the timezone of the crontab the same as your own timezone? You might find this happening at 2pm GMT.

Phil P

Posted 2009-12-10T08:38:25.970

Reputation: 1 773

+1 This answer is awesome - particularly noting the point about the script needs to being executable! Thanks! – FXQuantTrader – 2015-01-09T06:10:10.343

Very subtle explanation. My upvote – Fokwa Best – 2016-10-24T12:58:55.573

18

Making crontab running is easy only . Here I am going to say how to run crontab jobs. It is useful for anyone who is stuck on crontab.

*/1 * * * * cd /home/hacks && sh notify.sh

To make the script executable, we have to do:

chmod +x home/hacks/notify.sh

Here i run this script for every one minute ... By doing below script, you can write it in a log file to find whether its working

write log

*/1 * * * * cd /home/hacks && sh notify.sh>>test.log

send mail

*/1 * * * * cd /home/hacks && sh notify.sh>>test.log | mail -s "Hi this is example" user@domain.com

Aravind

Posted 2009-12-10T08:38:25.970

Reputation: 401

2isnt that "*/1 * * * * sh /home/hacks/notify.sh" will also work ? – user1179459 – 2015-09-25T01:14:32.173

5

4 hypothesis:

  • the cron daemon is not running (do a ps axfww | grep cron and check)

  • the notify-send is trying to send output to a terminal, or an X session -- but it is ran from within the cron environment and it does not know "who to talk to", so to speak.

  • your script is not executable

  • the home/ path in the crontab script is relative to the user the scripts gets executed as. Try using the full path

lorenzog

Posted 2009-12-10T08:38:25.970

Reputation: 1 962

1

Add export DISPLAY=:0 above the notify-send line in your script. This addresses lornezog's second point.

W_Whalley

Posted 2009-12-10T08:38:25.970

Reputation: 3 212

0

You have to open crontab by the following command:

crontab -u username -e (to edit) -l(to list) -r(to remove) 10(minutes) 8-15(hours) *(Day of month) *(month) 1,3,5(days of week) /path/to/script/script_name.sh

This will run your script once an hour from 8AM-3PM at 10 minutes past the hour every Monday, Wednesday and Friday.

ravindrakhokharia

Posted 2009-12-10T08:38:25.970

Reputation:

0

First of All, we need to edit the crontab with Command crontab -eand than Inside this Crontab add the Path of Executable script and in your Case like this * 14 * * * home/hacks/notify.sh >/dev/null 2>&1 .

Start /Stop / restart cron service

  • /etc/init.d/crond start /stop / restart
  • service crond start /stop /restart
  • systemctl stop crond.service

systemctl stop crond.service

kunal

Posted 2009-12-10T08:38:25.970

Reputation: 101

-2

quite simple, add following line at bottom of the crontab file via: sudo nano /etc/crontab

@reboot root cd /home/pi/node-sonos-http-api && npm start &

Martini7

Posted 2009-12-10T08:38:25.970

Reputation: 1

This does not seem to be an answer to the question. – Ljm Dullaart – 2018-12-28T19:00:09.560