Linux – Bash script that starts a process for 4 seconds, then kills it doesn’t work

bashcaptive-portallinux

I have a script that starts firefox for 4 seconds, then kills it. Firefox will automatically log into a captive portal, so I only need it to be open for 4 seconds, as soon as the wifi connects. I am on Ubuntu 13.04.

My problem seems to be that $pid isn't set.

firefox ; pid=$!
sleep 4
kill $pid

EDIT: removed set, and now it gives kill an invalid pid.

Best Answer

Your script does not work, because it waits until the firefox process has ended and afterwards executes pid=$!and the other command.

An easy way to do what you want is timeout:

timeout 4s firefox

It starts the program provided after the first argument and stops it after the time given as the first argument has passed.

Related Question