LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
 
Search this Thread
Old 02-26-2014, 12:43 AM   #1
Andy Alkaline
Member
 
Registered: Jun 2004
Location: Minnesota, USA
Distribution: Slackware32-stable, Debian-wheezy-amd64, LFS 7.1
Posts: 355

Rep: Reputation: 36
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;
 
Old 02-26-2014, 12:56 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,883

Rep: Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084Reputation: 2084
I am not sure I understand the point of the script?
Also, could you not just use cron?
 
Old 02-26-2014, 03:51 PM   #3
Andy Alkaline
Member
 
Registered: Jun 2004
Location: Minnesota, USA
Distribution: Slackware32-stable, Debian-wheezy-amd64, LFS 7.1
Posts: 355

Original Poster
Rep: Reputation: 36
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).
 
Old 02-26-2014, 04:02 PM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,431
Blog Entries: 1

Rep: Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144
Wow. Very nice script.

Here is my way of doing the same thing:

Code:
#script.sh
while true;
	do 
	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
	sleep 2s
done
Code:
[root]# timeout 6000s ./script.sh
 
Old 02-27-2014, 02:56 PM   #5
Andy Alkaline
Member
 
Registered: Jun 2004
Location: Minnesota, USA
Distribution: Slackware32-stable, Debian-wheezy-amd64, LFS 7.1
Posts: 355

Original Poster
Rep: Reputation: 36
Quote:
Originally Posted by szboardstretcher View Post
Code:
[root]# timeout 6000s ./script.sh
Cool, I didn't know about the 'timeout' command.

But I was trying to think of a way that I could 'kill' the script without killing or interrupting any child processes.
 
  


Reply

Tags
automation, bash, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Run a command every 5 minutes, hours, days ... LXer Syndicated Linux News 0 04-08-2012 08:50 PM
convert total no of seconds in the format hour minutes and seconds suchi_s Programming 15 03-15-2011 11:34 AM
[SOLVED] bash: Convert decimal to minutes:seconds? DJCharlie Programming 10 01-26-2011 07:05 PM
Perl: Turning a seconds-since-the-epoch number into "X days ago" or similar AlucardZero Programming 2 08-26-2010 05:59 PM
Bash script to run commands for specified number of seconds? devmoc Linux - Software 14 12-07-2009 06:19 PM


All times are GMT -5. The time now is 08:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration