LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to open application and after some time(time delay) automatically it should close (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-open-application-and-after-some-time-time-delay-automatically-it-should-close-4175463601/)

santosh vyduyla 05-27-2013 08:23 AM

how to open application and after some time(time delay) automatically it should close
 
firefox | kill -9 `ps -A | grep firefox`

while using above command firefox opening and immediately it getting closed instead of that i need delay between opening and closing of application,i have alreay tried below command:
firefox | sleep 5 | kill -9 `ps -A | grep firefox`

TB0ne 05-27-2013 09:50 AM

Quote:

Originally Posted by santosh vyduyla (Post 4959822)
firefox | kill -9 `ps -A | grep firefox`

while using above command firefox opening and immediately it getting closed instead of that i need delay between opening and closing of application,i have alreay tried below command:
firefox | sleep 5 | kill -9 `ps -A | grep firefox`

What are you trying to accomplish with this? You're starting firefox, and wanting it to run for only 5 seconds before killing it?? What is the point/goal? And you can just put the commands in a shell-script, and execute them, if that's really what you want:
Code:

firefox &
sleep 5
kill -9 `ps -A | grep firefox`


273 05-27-2013 09:59 AM

Might the last line be replaced with the shorter "pkill -9 firefox" or are there possible problems with this?

btmiller 05-27-2013 02:36 PM

Why do you need to "kill -9"? A simple kill should work and will allow the process to terminate cleanly (save existing state, etc.) rather than just terminating it abruptly. It might be best to make sure you get the correct firefox PID, just in case there are multiple copies running (e.g. using different user accounts. A generic script to do this would be something like:

Code:

#!/bin/bash

progtorun=$1

progtorun &
progpid=$!

sleep 30 # replace 30 with number of seconds to sleep

kill $progpid
sleep 5 # give it some time to die
kill -9 $progpid >& /dev/null # make sure it's dead.

Edit to add: you could make this script more robust by adding alarm signals in conjunction with wait to trap and see if the child process exits before the timer runs out. See this page for the basic idea.

santosh vyduyla 05-27-2013 11:09 PM

problem resolved
 
Quote:

Originally Posted by TB0ne (Post 4959874)
What are you trying to accomplish with this? You're starting firefox, and wanting it to run for only 5 seconds before killing it?? What is the point/goal? And you can just put the commands in a shell-script, and execute them, if that's really what you want:
Code:

firefox &
sleep 5
kill -9 `ps -A | grep firefox`


Santosh :just i want to write script which opens one application after another,but during that process first application should close before next application opens,hence i asked this question,anyway thanks your answer resolved my problem


All times are GMT -5. The time now is 11:22 PM.