[SOLVED] BASH HowTo: exit script cleanly after x number of seconds(minutes,hours,days,etc)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
BASH HowTo: exit script cleanly after x number of seconds(minutes,hours,days,etc)
As it is, this script is designed to exit cleanly after x number of seconds. The x is given as the first and only argument at run-time. I'm sure it would be quite easy for an experienced prog'ger to modify it so it would be more flexible (minutes, hours, days.. can't be specified; only seconds). I don't know if anyone would find this script useful so I'll post it here for review or modification if anyone finds interest in it.
The primary purpose of this script would be to run a command (or series of commands) that execute in under a few seconds. And then loop.
The 3 lines I'm executing are not important (shown in comments).
Warning: there's no error checking in this script.
Code:
#!/bin/bash
function get_cur_sec() {
date +%s
}
# How long the script should run for.
duration=$1
CURSEC=`get_cur_sec`
quitpoint=`expr $CURSEC + $duration`
# 3 lines below for debugging
#echo $quitpoint
#echo $CURSEC
#exit 0;
while [ $CURSEC -lt $quitpoint ];
do
# The 3 lines below should be replaced with commands
# you'd like executed.
echo -e "MotionNotify 1128 286" | /usr/bin/xmacroplay :0
echo -e "ButtonPress 1" | /usr/bin/xmacroplay :0
echo -e "ButtonRelease 1" | /usr/bin/xmacroplay :0
# the 2 lines below are for debugging
echo $quitpoint
echo $CURSEC
# sleep value can be customized
sleep 2s
CURSEC=`get_cur_sec`
done
exit 0;
I don't think cron would have been appropriate in my case.
The 3 commands I'm executing in this script simply deletes many tags from an extremely long list on my wordpress reader dashboard. It clicks an "X" over and over again in the same area every 2 seconds.
I know another method of doing what this script does is schedule a kill command with at or cron, but a script with a "built-in kill switch" just seemed a little cleaner, and of course more fun (to code).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.