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.
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 ?
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.
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.
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.
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.
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
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.
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662
Rep:
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
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'?
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662
Rep:
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
--------------------------------------------------
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662
Rep:
[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]#
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.