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?

dearN
  • 2,179

3 Answers3

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

muru
  • 197,895
  • 55
  • 485
  • 740
RobotHumans
  • 29,530
  • 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 01 '12 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 01 '12 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 01 '12 at 20:49
  • No idea on the 5 second lag. – RobotHumans Apr 01 '12 at 20:51
  • 3
    I love the way you went through this step by step and then summarized it--thanks! – jbobbins Dec 29 '16 at 17:13
  • still not working =( – Madeo Feb 19 '20 at 07:08
  • I had a script using kubectl under the hood that was working just fine on the shell, but failing with The connection to the server localhost:8080 was refused kubectl. The issue was falling into that env variable not set category, so I had to add to my crontab KUBECONFIG=/path_to/kube_config_cluster.yml which I copied from echo $KUBECONFIG. fwiw – Jean-Frederic PLANTE Mar 31 '22 at 20:08
  • After 2+ days of grinding through why this cron was not working the path issues resolved this for me. Thank you for putting this together!

    ..also new empty line at the end of file.

    – Jordan Jul 29 '22 at 21:15
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.

muru
  • 197,895
  • 55
  • 485
  • 740
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

Vadim
  • 121
  • 4