LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-28-2011, 12:04 PM   #1
Jajamd
Member
 
Registered: Aug 2004
Posts: 37

Rep: Reputation: 0
Script to restart a program at a given different interval


Hello guys,

I've been trying to find some utility that would allow me to restart a program at a given interval, let's say 3minutes.

I know "watch" is supposed to do that but it doesn't seem to work...

Let's take for instance: firefox

watch -n 180 killall firefox-bin && /usr/bin/firefox

It works once, but then nothing... it won't kill it once it's launched. I don't really know what's wrong and how I can solve this issue.

Please let me know if you know any other program that would suit my need.

Thank you very much
 
Old 12-28-2011, 12:44 PM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by Jajamd View Post
watch -n 180 killall firefox-bin && /usr/bin/firefox
This is actually two commands, the first of which reads "every 180 seconds, kill all processes named firefox-bin". The second one, "start /usr/bin/firefox", is only run if the first one exits successfully. Since watch will only exit when you interrupt it (Ctrl+C), you effectively just kill your Firefox every three minutes.

To run two or more commands using watch, you need to use quotes. For example,
Code:
watch -n 180 'killall firefox-bin && /usr/bin/firefox'
However, you can just use a shell loop instead:
Code:
bash -c 'while [ 1 ]; do killall firefox-bin ; /usr/bin/firefox ; sleep 180 ; done </dev/null &>/dev/null'
watch is a terminal application, this one is a simple shell command; it is better suited for example to the desktop environment startup commands. The redirections at the end hide Firefox error messages from output, in case you run it on the command line.

If you want to make sure Firefox is killed if the script is interrupted or fails, use
Code:
bash -c 'trap "killall -KILL firefox-bin" EXIT ; while [ 1 ]; do killall -TERM firefox-bin ; /usr/bin/firefox ; sleep 180 ; done </dev/null &>/dev/null'
If you intend this to be used in a kiosk-type setup, perhaps a bit more complex script is in order. For example, you could use an inactivity timeout (instead of a fixed period), kill Firefox, and then remove the current profile, restoring a known safe profile, before restarting Firefox. That makes sure that even if the user manages to install a malware toolbar or extension, it is discarded at the next restart.
 
1 members found this post helpful.
Old 12-28-2011, 01:50 PM   #3
Jajamd
Member
 
Registered: Aug 2004
Posts: 37

Original Poster
Rep: Reputation: 0
Thank you very much Nominal Animal ! I've made some progress but something still isn't working. When I enter either one of the shell commands you gave me, firefox still won't be killed after 180secs. It only works if I type killall firefox-bin manually. But the good news is that it will restart automatically. Why does this part of the shell command (/usr/bin/firefox) works while the killall part doesn't?

Cheers !
 
Old 12-28-2011, 02:54 PM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Recent versions of Firefox no longer use firefox-bin, just firefox. Use
Code:
bash -c 'while [ 1 ]; do killall firefox ; /usr/bin/firefox ; sleep 180 ; done </dev/null &>/dev/null'
instead.

If you are unsure, start Firefox normally, then run
Code:
ps axfu | sed -ne '1 p; / sed / d; /firefox/ p'
to list all processes that contain firefox in the command line (but not sed).
 
Old 12-28-2011, 05:23 PM   #5
Jajamd
Member
 
Registered: Aug 2004
Posts: 37

Original Poster
Rep: Reputation: 0
Thank you for taking the time to explain everything Nominal.

Cheers !
 
  


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
Bash script to restart a dead script nathanpayne Programming 9 07-22-2011 10:28 PM
Linux Shell Script to GET an HTML page repeatedly with an Interval vxc69 Programming 5 05-08-2007 09:43 AM
Way to automatically restart a program? AlexV Linux - General 1 04-13-2005 05:23 PM
Restart a program sibtay Programming 4 09-28-2004 10:46 AM

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

All times are GMT -5. The time now is 03:01 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