How to execute shell script via crontab?

Non release banter
User avatar
Slaxmax
Contributor
Contributor
Posts: 408
Joined: 03 Jan 2013, 09:51
Distribution: KDE4
Location: Campinas Brazil https://goo.gl/yrxwKi

How to execute shell script via crontab?

Post#1 by Slaxmax » 30 Aug 2014, 16:35

I tried a few methods suggested in google, but none of them worked.
“DNA is like a computer program but far, far more advanced than any software ever created.”
― Bill Gates, The Road Ahead

User avatar
brokenman
Site Admin
Site Admin
Posts: 6105
Joined: 27 Dec 2010, 03:50
Distribution: Porteus v4 all desktops
Location: Brazil

Re: How to execute shell script via crontab?

Post#2 by brokenman » 30 Aug 2014, 21:35

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
How do i become super user?
Wear your underpants on the outside and put on a cape.

User avatar
Slaxmax
Contributor
Contributor
Posts: 408
Joined: 03 Jan 2013, 09:51
Distribution: KDE4
Location: Campinas Brazil https://goo.gl/yrxwKi

Re: How to execute shell script via crontab?

Post#3 by Slaxmax » 30 Aug 2014, 22:49

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?
“DNA is like a computer program but far, far more advanced than any software ever created.”
― Bill Gates, The Road Ahead

User avatar
brokenman
Site Admin
Site Admin
Posts: 6105
Joined: 27 Dec 2010, 03:50
Distribution: Porteus v4 all desktops
Location: Brazil

Re: How to execute shell script via crontab?

Post#4 by brokenman » 30 Aug 2014, 23:55

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
How do i become super user?
Wear your underpants on the outside and put on a cape.

User avatar
Slaxmax
Contributor
Contributor
Posts: 408
Joined: 03 Jan 2013, 09:51
Distribution: KDE4
Location: Campinas Brazil https://goo.gl/yrxwKi

Re: How to execute shell script via crontab?

Post#5 by Slaxmax » 31 Aug 2014, 00:39

Does not work for root too.
“DNA is like a computer program but far, far more advanced than any software ever created.”
― Bill Gates, The Road Ahead

User avatar
brokenman
Site Admin
Site Admin
Posts: 6105
Joined: 27 Dec 2010, 03:50
Distribution: Porteus v4 all desktops
Location: Brazil

Re: How to execute shell script via crontab?

Post#6 by brokenman » 31 Aug 2014, 02:30

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"
How do i become super user?
Wear your underpants on the outside and put on a cape.

User avatar
Slaxmax
Contributor
Contributor
Posts: 408
Joined: 03 Jan 2013, 09:51
Distribution: KDE4
Location: Campinas Brazil https://goo.gl/yrxwKi

Re: How to execute shell script via crontab?

Post#7 by Slaxmax » 31 Aug 2014, 03:37

Image

Thanks!
“DNA is like a computer program but far, far more advanced than any software ever created.”
― Bill Gates, The Road Ahead

User avatar
francois
Contributor
Contributor
Posts: 6434
Joined: 28 Dec 2010, 14:25
Distribution: xfce plank porteus nemesis
Location: Le printemps, le printemps, le printemps... ... l'hiver s'essoufle.

Re: How to execute shell script via crontab?

Post#8 by francois » 31 Aug 2014, 21:12

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
Prendre son temps, profiter de celui qui passe.

User avatar
Slaxmax
Contributor
Contributor
Posts: 408
Joined: 03 Jan 2013, 09:51
Distribution: KDE4
Location: Campinas Brazil https://goo.gl/yrxwKi

Re: How to execute shell script via crontab?

Post#9 by Slaxmax » 31 Aug 2014, 23:21

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.
“DNA is like a computer program but far, far more advanced than any software ever created.”
― Bill Gates, The Road Ahead

Post Reply