myol
myol

Reputation: 9858

Briefly run / Restart sublime text from terminal

I don't want to run an external program (subl - sublime text) at a certain point of time, I want to run it for a certain amount of time. I basically need to boot up the program for 10 seconds then kill it - multiple times - because of its install and update process.

How can I do this?

Upvotes: 0

Views: 301

Answers (3)

myol
myol

Reputation: 9858

I ended up using this in my script

/usr/bin/subl
SPID="$(ps -A | grep sublime_text | awk '{print $1}')"
sleep 5
kill "$SPID"
unset "$SPID"

Upvotes: 0

sat
sat

Reputation: 14949

Use timeout:

timeout 5s <program>

You can also specify the signal which need to be passed to terminating the process.

timeout -s9 5s <program>

(OR)

timeout --signal=KILL 5s <program>

Test:

$ time timeout 5s sleep 40

real    0m5.002s
user    0m0.001s
sys     0m0.001s

Upvotes: 2

chepner
chepner

Reputation: 531888

You may have a timeout command on your system, which uses a standard alarm signal to terminate a process. I've never quite understood why no shell provides access to this feature as a builtin. If you don't have timeout on your system, you can simulate it with

my_program & pid=$!
sleep 10
kill "$pid"

Upvotes: 2

Related Questions