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

6,072

Solution 1

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.

Solution 2

The reason your pid=$! fails is that $! is the PID of last job run in background.

I.e.,

$ foo & echo $!

will start foo and echo the PID of its process.

In your case, firefox ; pid=$! would need to be replaced with firefox & pid=$! but it might be pretty useless because firefox is a script which execs the actual binary.

What you need to do is either use killall (which will try to kill all the instances firefox, whether yours or other users') or (copy and) edit the /usr/bin/firefox script to echo the new PID.

Share:
6,072

Related videos on Youtube

Yet Another User
Author by

Yet Another User

Just another user that wants to help others

Updated on September 18, 2022

Comments

  • Yet Another User
    Yet Another User almost 2 years

    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.

    • Cristian Ciupitu
      Cristian Ciupitu over 10 years
      N.B. $! is the PID (process ID) of last job run in background, so you'd have to replace ; with & at the very least. Not that this would solve your main problem.
    • ganesh
      ganesh over 10 years
      I know that does does not answer the question, but why use firefox to log in via the captive portal? That is like going rowing with a battleship. Wget or curl are much more lightweight. (Link to curl example).
    • mpy
      mpy over 10 years
    • Tim
      Tim over 10 years
      @Hennes I also wanted to login to a captive portal. Wget and curl failed for me (maybe because of javascript elements or similar things). I finally succeeded using elinks. Not as lightweight as curl or wget, but much better than firefox.
    • Yet Another User
      Yet Another User over 10 years
      @Tim The problem here is that it has some fancy javascript that needs running so it will work.
  • Cristian Ciupitu
    Cristian Ciupitu over 10 years
    @YetAnotherUser: you don't have a bunch of instances (processes), you have only one process displaying multiple windows and tabs. I'd suggest using a separate profile just for this, if possible.
  • Yet Another User
    Yet Another User over 10 years
    Separate profile? Hmmm yeah. I hacked together a long string of commands that gets the pid. Will post it shortly as soon as I get it completely working.
  • Dean
    Dean over 10 years
    A handy command to get a process id is pgrep.
  • terdon
    terdon over 10 years
    As Dean suggested, using pgrep firefox removes all the "string-processing mess". And really, using firefox for this is silly, slow and resource intensive. Just use curl or wget as Hennes suggested.
  • Yet Another User
    Yet Another User over 10 years
    @terdon I HAVE tried. Their silly captive portal runs some javascript that I can't seem to reverse-engineer the POST requests for. I have looked at it using wireshark, and my attempt to make it do what it's supposed to do fails horribly. It might be checking for if the browser has visited the captive portal page before actually doing anything.
  • jaychris
    jaychris over 10 years
    What is timeout? I don't think it is part of standard Linux installations.
  • terdon
    terdon over 10 years
    Fair enough, using firefox is much easier than deciphering other people's code.
  • Tim
    Tim over 10 years
    @jaychris It comes with coreutils, so it should be part of a standard installation.
  • ganesh
    ganesh over 10 years
    It seems to be missing on my Ubuntu server. (Not the most recent Ubuntu, but apt-get easily fixed that).
  • ganesh
    ganesh over 10 years
    Aside from the danger of killall (kill all firefoxes on Linux, kill a lot more Solaris), you might want to add a & after the firefox command. Else your sleep will not start until after you closed firefox.
  • Yet Another User
    Yet Another User over 10 years
    killing firefox puts it in safe mode. That is undesirable.
  • Yet Another User
    Yet Another User over 10 years
    @terdon Especially when they don't really care about it being readable to others. Even more so, when they kind of don't want people reading it, as that could mess with their security.
  • sds
    sds almost 10 years
    why downvote?..
  • edencorbin
    edencorbin about 6 years
    so awesome, shell scripting in Jenkins and had a show logs command that could only be control Zed to exit, except timeout, that works!