Page 1 of 1

How to execute shell script via crontab?

Posted: 30 Aug 2014, 16:35
by Slaxmax
I tried a few methods suggested in google, but none of them worked.

Re: How to execute shell script via crontab?

Posted: 30 Aug 2014, 21:35
by brokenman
I just tested this which works.

As guest create a script at /home/guest/testjob like so:

Code: Select all

echo "echo cronjob ran at \`date +%s\` > /tmp/out" > /home/guest/testscript
Make it executable.

As root run:

Code: Select all

crontab -e -u guest
Press insert
Add your conjob
28 18 30 08 * /home/guest/testjob
Press Esc
Type: :wq
Press Enter

Wait until the job runs at: 18:28 on August 30th and check /tmp/out

In this line the meanings are:
28 – 28th Minute
18 – 06 PM
30 – 30th Day
08 – 8th Month (August)
* – Every day of the week

Re: How to execute shell script via crontab?

Posted: 30 Aug 2014, 22:49
by Slaxmax
Thanks

Your script works, but this no...

Code: Select all

#!/bin/bash
/usr/bin/notify-send "hi welcome" && echo "blabla" > /home/guest/test$$
or other "more complex" script.

this script write test$$, but no execute notify-send

Why?

Re: How to execute shell script via crontab?

Posted: 30 Aug 2014, 23:55
by brokenman
In this case cron probably doesn't have access to the DBUS_SESSION_BUS_ADDRESS environment variable (as root). Also you may need to run notify-send as: sudo -u guest notify-send

Re: How to execute shell script via crontab?

Posted: 31 Aug 2014, 00:39
by Slaxmax
Does not work for root too.

Re: How to execute shell script via crontab?

Posted: 31 Aug 2014, 02:30
by brokenman
This worked for me:

Code: Select all

guest@porteus:~$ crontab -l
23 23 30 08 * export DISPLAY=:0.0 && /usr/bin/notify-send "Your cronjob called notify-send directly"
also

Code: Select all

guest@porteus:~$ crontab -l
26 23 30 08 * export DISPLAY=:0.0 && /home/guest/testcron
guest@porteus:~$ cat /home/guest/testcron
/usr/bin/notify-send "Your cronjob has run from a script"

Re: How to execute shell script via crontab?

Posted: 31 Aug 2014, 03:37
by Slaxmax
Image

Thanks!

Re: How to execute shell script via crontab?

Posted: 31 Aug 2014, 21:12
by francois
Slaxmax:

I think that smilies of yours should be added to the smilies table of the forum. I wonder if one of the dev could do that. :D

Re: How to execute shell script via crontab?

Posted: 31 Aug 2014, 23:21
by Slaxmax
francois wrote:Slaxmax:

I think that smilies of yours should be added to the smilies table of the forum. I wonder if one of the dev could do that. :D
I agree. Very cool.