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.
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.
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
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.
s not fine for me. I
m 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:58if (( $minutes > 20 )); then ...
(if you use integers)
– muru
Oct 20 '15 at 14:32
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
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
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
/etc/rc.local
. – Wilf Oct 20 '15 at 13:31