74

I have a script that reminds me to restart my computer if uptime is more than, say 3 days (although its set to 0 days now just to check if the script is running as my computer has been up only over a day..).

I realize it isn't the most elegant script but I am trying! :)

#!/bin/bash

up=$(uptime | grep "day" > /home/username/uptime.foo && awk < /home/username/uptime.foo '{ print $3 }')

[[ $up -gt 0 ]] && xmessage -center "Restart!"

I have made it executable by chmod + x checkup.sh and it works fine when I run it fro the terminal via ./checkup.sh

My crontab entry for this script is:

46 14 * * * /home/username/Desktop/./checkup.sh

So it runs at 14:46hrs daily...

So... I am thinking it should run, unless I didn't something really silly. Also, do you think it's ok to move this bash script to /bin?

7
  • 1
    I would put it in /opt/bin or ~/bin as a general best practice to not muck up where packages think things are. Apr 1, 2012 at 18:55
  • If you run the script like so: /home/dnaneet/Desktop/./checkup.sh does it work? I would put /home/dnaneet/Desktop/checkup.sh
    – LnxSlck
    Apr 1, 2012 at 18:59
  • @LnxSlck Yes, running /home/dnaneet/Desktop/./checkup.sh works fine. But /home/dnaneet/Desktop/checkup.sh may not work right? Because it needs a ./?
    – dearN
    Apr 1, 2012 at 19:03
  • 1
    I don't think cron knows anything about your X session. Apr 1, 2012 at 19:34
  • @glennjackman So what does that mean? Does it mean that running such a script through crontab doesn't allow a pop up message to show up in the gnome environment?
    – dearN
    Apr 1, 2012 at 19:38

3 Answers 3

84

One thing at a time:

First let's give you a user based bin folder:

cd ~/ && mkdir bin

You want to use crontab. Let's start with something really simple:

* * * * * touch /tmp/testing.txt

Okay, so that works

Now let's try running a script that does the same

* * * * * /home/username/bin/touchtest.sh

to run once a minute until you get it working
No you don't need a ./ in the middle of the line. ./ is for when you are giving relative urls.
Okay, so that works

Now let's try running a script that calls xmessage

* * * * * /home/username/bin/rebootwarn.sh

not working

First we need to not depend on environment variables. This includes path setting, x11 settings, or anything else(python and ruby environment variables come to mind...)

Let's make ours look a bit like anacron's proper cron file..I saved this as test

#Borrowed from anacron
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
#End borrowed from anacron

* * * * *   /bin/bash /home/username/bin/test.sh

Set to run once a minute

crontab test to import it

On to the script

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
export DISPLAY=:0.0
up=$(uptime | grep "day" > /home/dnaneet/uptime.foo && awk < /home/dnaneet/uptime.foo '{ print $3 }')

[[ $up -gt 0 ]] && xmessage -center "Restart!"`

Okay, so that works...what did we do?
We changed all the commands not to depend on paths we didn't explicitly set
We ran our script explicitly with bash
We told the script that we expect to be on DISPLAY :0.0

8
  • 2
    Thanks for the answer. You said "Let's make ours look a bit like anacron's proper cron file..I saved this as test".. OUR what? The crontab file? :-/
    – dearN
    Apr 1, 2012 at 20:23
  • Heres what I observed. First I had to get rid of the space before and after = for the line specifying DISPLAY. Second, when I edited my crontab file and added the PATH as suggested and set the time to run at a later time, the pop up window didn't show. However, the popup window showed fine when running the script using /bin/bash... what gives?
    – dearN
    Apr 1, 2012 at 20:34
  • What do you know, once I changed my script to export DISPLAY=:0.0, it worked fine. Although there was a significant lang of about 5 seconds after the clock chimed the hour when my pop up window should have showed... Any suggestions? But yes, your amends to the script work!
    – dearN
    Apr 1, 2012 at 20:49
  • No idea on the 5 second lag. Apr 1, 2012 at 20:51
  • 3
    I love the way you went through this step by step and then summarized it--thanks!
    – jbobbins
    Dec 29, 2016 at 17:13
21

The problem was solved read the manual in Google:

description:

  1. I have script using #!/bin/bash as header
  2. I put the script on /home/wc3/palert/

analysis:

  1. crontab can't run my script
  2. But if I run manually it show the output and also I can see the result on my web
  3. It means crontab can't get the environment not like when you run your script on your script folder

answer:

  1. put your environment using this line below on your script with:

    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/wc3/palert
    

    change this: /home/wc3/palert
    with: the place that you put your script e.g. /home/budi/script.sh

sample:

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/budi
# Script continues...

Try put your script on crontab.

4
  • doesn't work for me
    – John Joe
    Mar 21, 2017 at 4:15
  • Work like a charm for me! Sep 27, 2019 at 6:38
  • it worked! thank you! Nov 14, 2019 at 22:02
  • it should be the accepted answer.
    – Farukest
    Jun 27, 2022 at 21:20
1

In my case all was cause it is not loaded profile on a first place. So just use -l or --login. Like so @reboot bash -l /path/to/scirpt.sh >> /tmp/some.log

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .