LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 07-09-2006, 01:11 PM   #1
spx2
Member
 
Registered: Dec 2005
Distribution: debian
Posts: 160

Rep: Reputation: 30
simple script


i want to make a bash script
to execute an application for n seconds and
then stop it,then execute it again for n seconds
and so on.
how can i approach this problem ?
 
Old 07-09-2006, 01:46 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
You can run the program as a background job, sleep for the interval you want, then kill the process. The special variable '$!' contains the process number for the last background job submitted.
 
Old 07-09-2006, 01:52 PM   #3
spx2
Member
 
Registered: Dec 2005
Distribution: debian
Posts: 160

Original Poster
Rep: Reputation: 30
so you suggest something like

bg application
$pidapp=$!
fg application
sleep n seconds
kill -SIGTERM $pidapp

?

please tell me correct syntax
 
Old 07-09-2006, 02:28 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
This seems to do it.

Code:
#! /bin/bash

while [ 1 ] ;
do
        $1 &
        repeatpid=`jobs -p`;
        echo "Current job: $repeatpid";
        sleep 10;
        kill -9 $repeatpid;
done
--- rod.
 
Old 07-09-2006, 02:30 PM   #5
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
I'd suggest
Code:
while true; do timeout -s TERM n application; done
 
Old 07-09-2006, 02:41 PM   #6
spx2
Member
 
Registered: Dec 2005
Distribution: debian
Posts: 160

Original Poster
Rep: Reputation: 30
bash: timeout: command not found

this is the error i get.
i'm using Vector Linux SOHO 5.1 wich has allot if not all linux
bash utils or usual commands.
i am root so i have access to all of them.
 
Old 07-09-2006, 02:42 PM   #7
spx2
Member
 
Registered: Dec 2005
Distribution: debian
Posts: 160

Original Poster
Rep: Reputation: 30
bash: timeout: command not found

this is the error i get.
i'm using Vector Linux SOHO 5.1 wich has allot if not all linux
bash utils or usual commands.
i am root so i have access to all of them.
 
Old 07-09-2006, 03:02 PM   #8
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Oh, you're right. I thought it was standard, but it belongs to a package called netatalk here. So you're probably better off with theNbomr's script, only that it's a little easier to get the PID of the backgound process in Bash:
Code:
while true; do application& sleep n; kill -s TERM $!; done
Edit: OK, now I've got it.

Last edited by spirit receiver; 07-09-2006 at 03:17 PM.
 
Old 07-10-2006, 06:14 AM   #9
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
Using the vi editor, I wrote the following file which has the name 'kill100'.
---------------------------------

#! /bin/bash

while [ 1 ] ;
do
$1 &
repeatpid=`jobs -p`;
echo "Current job: $repeatpid";
sleep 10;
kill -9 $repeatpid;
done
------------------------------------

When I ran the program, it was a non-stopping output. Please read the following:

[nissanka@c83-250-110-112 ~]$ vi kill100
[nissanka@c83-250-110-112 ~]$ chmod 755 kill100
[nissanka@c83-250-110-112 ~]$ ./kill100
Current job: 7928
./kill100: line 12: kill: (7928) - No such process
Current job: 7931
./kill100: line 12: kill: (7931) - No such process
Current job: 7934
./kill100: line 12: kill: (7934) - No such process
Current job: 7937
./kill100: line 12: kill: (7937) - No such process
Current job: 7940
./kill100: line 12: kill: (7940) - No such process
Current job: 7943
./kill100: line 12: kill: (7943) - No such process
Current job: 7946

-----------------------------------------------------

How can I terminate it?
I think the following line will help to terminate the program.
while true; do application& sleep n; kill -s TERM $!; done

Where shall I place it on the program?
 
Old 07-10-2006, 08:50 AM   #10
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
The script runs forever. To kill it, type Control-C from the script's terminal, or use ps to find it's pid, and issue a 'kill -9' to it. However, in your example, you failed to give the script the name of any program to run. It was written with the intent to provide the program name as a commandline argument.

Code:
[nissanka@c83-250-110-112 ~]$ ./kill100 yourProgram
Your specification in post #1 did not specify that it should terminate.

--- rod.
 
Old 07-10-2006, 12:44 PM   #11
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
Thanks theNbomr for taking time to reply. I am running the wordprocessor program 'Open office'.

Let us say that I want to kill it or rather discontinue it using this program. How do I find the name of the program?

The following didn't work. I just wrote the words 'openoffice' and 'open office'.


[nissanka@c83-250-110-112 ~]$
[nissanka@c83-250-110-112 ~]$ ps
PID TTY TIME CMD
7845 pts/1 00:00:00 bash
15327 pts/1 00:00:00 ps
[nissanka@c83-250-110-112 ~]$ ./kill100 open office
Current job: 15329
./kill100: line 12: kill: (15329) - No such process
Current job: 15333
./kill100: line 12: kill: (15333) - No such process
Current job: 15337
[nissanka@c83-250-110-112 ~]$ ./kill100 openoffice
./kill100: line 8: openoffice: command not found
Current job: 15342
./kill100: line 12: kill: (15342) - No such process
./kill100: line 8: openoffice: command not found
Current job: 15345
./kill100: line 12: kill: (15345) - No such process
./kill100: line 8: openoffice: command not found
Current job: 15348
./kill100: line 12: kill: (15348) - No such process

Last edited by Gins; 07-10-2006 at 12:46 PM.
 
Old 07-10-2006, 01:20 PM   #12
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
On my system, OpenOffice Writer is called oowriter. It is installed in /usr/bin on my system, so it is found in my usual PATH. You should try
Code:
kill100 oowriter
If that fails, use
Code:
find / -name oowriter -print
to find out where your OpenOffice word processor is. Then use that to specify the full filespec to your kill100 script. Or, use the desktop menu editor to inspect the menu item for OpenOffice, to locate the binary filename, or use 'locate oowriter' if you have the slocate database installed. It may take longer than 10 seconds for the program to fully initialize, so killing it before then just might cause Bad Things to happen. Killing a word processor ungracefully like that is probably not the ideal practice, anyway. Why don't you try this experiment using something a little less dramatic, like, say 'xterm'?

--- rod.
 
Old 07-10-2006, 02:04 PM   #13
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
Thanks theNbomr for the reply.

The command just went on opening a blank page of open office. Why was that? I just closed the shell to avoid creating more empty pages of open office. Afterwards, I closed all the blank pages one by one.

[nissanka@c83-250-110-112 ~]$ ./kill100 oowriter
Current job: 16184
./kill100: line 12: kill: (16184) - No such process
Current job: 16207
./kill100: line 12: kill: (16207) - No such process
Current job: 16230
./kill100: line 12: kill: (16230) - No such process
Current job: 16253
./kill100: line 12: kill: (16253) - No such process
Current job: 16276
./kill100: line 12: kill: (16276) - No such process
Current job: 16299
--------------------------------------------------

[nissanka@c83-250-110-112 ~]$ su root
Password:
[root@c83-250-110-112 nissanka]# find / -name oowriter -print
/usr/bin/oowriter
[root@c83-250-110-112 nissanka]#
 
Old 07-10-2006, 02:06 PM   #14
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
[root@c83-250-110-112 nissanka]# slocate oowriter
warning: slocate: warning: database /var/lib/slocate/slocate.db' is more than 8 days old
/usr/share/doc/mandriva/en/Starter/Starter.html/images/oowriter-bulletlist-icon.png
/usr/share/doc/mandriva/en/Starter/Starter.html/images/oowriter-numberedlist-icon.png
/usr/share/doc/mandriva/en/Starter/Starter.html/images/oowriter-page-styles.png
/usr/share/doc/mandriva/en/Starter/Starter.html/images/oowriter-stylist-icon.png
/usr/share/mimelnk/application/x-oowriter.desktop
/usr/bin/oowriter
[root@c83-250-110-112 nissanka]#





[root@c83-250-110-112 nissanka]# locate oowriter
warning: locate: warning: database /var/lib/slocate/slocate.db' is more than 8 days old
/usr/share/doc/mandriva/en/Starter/Starter.html/images/oowriter-bulletlist-icon.png
/usr/share/doc/mandriva/en/Starter/Starter.html/images/oowriter-numberedlist-icon.png
/usr/share/doc/mandriva/en/Starter/Starter.html/images/oowriter-page-styles.png
/usr/share/doc/mandriva/en/Starter/Starter.html/images/oowriter-stylist-icon.png
/usr/share/mimelnk/application/x-oowriter.desktop
/usr/bin/oowriter
[root@c83-250-110-112 nissanka]#



How do I use the Xterm? I have never used it.

Last edited by Gins; 07-10-2006 at 02:11 PM.
 
Old 07-10-2006, 02:15 PM   #15
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
oowriter is just a little script that starts another program and exits. And I don't see much sense in using the script "kill100" with oowriter.
 
  


Reply



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
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 04:19 AM
Need help with a simple script shell script WindowBreaker Linux - Software 2 12-15-2005 12:45 PM
A very simple script roopunix Linux - Networking 3 05-28-2004 05:30 AM
A Simple Script fiod Linux - General 3 09-20-2003 08:56 PM
Simple C Shell script is not so simple elconde Programming 2 09-16-2001 11:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:02 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
Open Source Consulting | Domain Registration