1

I need run BASH script when uptime >= 20 min(for example). How I can do it?

Please help me create script for check system uptime and check if uptime more 20 min - run another script.

Thanks.

  • Do you need cron? – choroba Oct 20 '15 at 13:27
  • 4
    You could add a command with a delay (or delay a command) on boot from /etc/rc.local. – Wilf Oct 20 '15 at 13:31
  • 1
    Please be more specific. Do you need to run the script as soon as the uptime is 20 minutes, or (as one of your comments suggests:) do you need to run it whenever the user invokes it and the uptime is at least 20 minutes? – Reinier Post Oct 20 '15 at 17:46

2 Answers2

10

There is no need to know when the 20 minutes have passed. The command "at" can be used to just tell the system to do something 20 minutes from a certain moment (and that moment can be a (re)boot).

In /etc/crontab put @reboot {user} /usr/local/bin/script.sh and make /usr/local/bin/script.sh executable. Change {user} to a user name or "root.

Create /usr/local/bin/script.sh and when you put this in it...

 at -f /usr/local/bin/script2.sh now + 20 minutes

/usr/local/bin/script2.sh will be executed 20 minutes after (re)booting. The actual time frame might vary a bit (since the booting process will probably already by on-going before "crontab" gets executed. Just an example but this is a general method in creating such an event.

/usr/local/bin/script2.sh can used to add in more lines with similar starting points (40 minutes from reboot etc.)

This will survive a reboot! So 5 reboots within 20 minutes = 5 times executing it. Hmmm... leaving it in here though.


Another approach:

If you need to do this from a script itself you can also create a variable to be included into the script. 1st a simple example:

$uptime
 16:11:31 up 1 day,  7:35,  2 users,  load average: 0,10, 0,07, 0,06
~/tmp$ more test
uptime
~/tmp$ more test2
minutes=20;
at -f test now + $minutes minutes

And executing ./test2 will show

warning: commands will be executed using /bin/sh
job 7 at Tue Oct 20 16:29:00 2015

Now all you need to do is change "minutes" to a variable based on for instance "uptime".

This is an example on getting the uptime in minutes in the variable "minutes":

 minutes=`awk '{print $0/60;}' /proc/uptime`;

And this is a method to making it conditional:

#!/bin/bash
minutes=`awk '{print $0/60;}' /proc/uptime`;
if [ $(echo "$minutes > 20" | bc) -ne 0  ]
then
  at -f test now + 20 minutes
fi
  • If minutes greater than 20 execute script in 20 minutes from now.

Using pure bash:

#!/bin/bash
read -d. seconds < /proc/uptime
if (( $seconds > 20*60 ))
then
  at -f test now + 20 minutes
fi

This should get you started.

muru
  • 197,895
  • 55
  • 485
  • 740
Rinzwind
  • 299,756
  • Sorry, but its not fine for me. Im need run script without Cron. For example: in any manual time I`m run script(this script need to check if uptime >= 20 min - run another script, else - exit)

    Script can run in 5 min uptime, or in 25 min uptime. But in 5 min uptime.

    – Petro Pikh Oct 20 '15 at 13:58
  • 7
    @PetroPikh I am puzzled to see how you get a beautiful answer after providing no effort whatsoever and you even dare to complain about it. – fedorqui Oct 20 '15 at 14:02
  • 1
    The conditional doesn't need bc: Use if (( $minutes > 20 )); then ... (if you use integers) – muru Oct 20 '15 at 14:32
  • @muru minutes is a float and I did not find a good method without using "bc". Feel free to mutilate my attempt ;-) – Rinzwind Oct 20 '15 at 15:00
  • 2
    The problem with the use of at is that it assumes that 20 minutes after boot time the uptime will be 20 minutes. If for some reason there is another reboot, say, 15 minutes after the first one, the job will run with less than 5 minutes' uptime. – Monty Harder Oct 20 '15 at 15:04
  • @Rinzwind mutilated accordingly. – muru Oct 20 '15 at 15:09
  • @MontyHarder check. True and added it :D – Rinzwind Oct 20 '15 at 15:11
  • 2
    @MontyHarder I think the simplest solution to that is to avoid the use of at altogether. The script can simply start a subshell in the background and let that subshell sleep for 20 minutes before doing anything. – kasperd Oct 20 '15 at 16:11
3

To run a script in 20 minutes after the boot, put into /etc/rc.local:

nohup your-script >/dev/null 2>&1 &

where your-script has sleep 1200 in the beginning of the script.

Or create an upstart job or specify @reboot cron command or configure "Startup Applications", etc depending on what is the most appropriate in your case, see How to run scripts on start up?


To run one script if uptime is greater than 20 minutes and another one otherwise, in bash:

read -d. uptime < /proc/uptime
if (( uptime > 1200 ))
then
   one_script
else
   another_script
fi
jfs
  • 4,008