Search code examples
phpbashphirehose

How to start/end a background process from PHP/bash script?


I have a service where I list Twitter accounts and have a switch to enable/disable a Streaming API connection for them.

Currently I'm planning to have this flow:

  • User enables a Twitter account for Streaming
  • An email is sent to an admin with cli access on the server, who launches a Phirehose process for that account.
  • (A cron job takes a list of Twitter accounts that have Streaming enabled and checks if a process exists for them. If not, it sends an email to the admin who can relaunch it.)

I would like to launch a Phirehose process directly to run in the background when a user wants a Twitter account to be enabled for Streaming. Also I would like to kill the process directly if a user wants to stop Streaming for that account.

There are several risks and concerns with executing the Phirehose process and especially killing it in an automated way.

Any tips on the best way to implement this with maximal automation and minimal admin interference?


Solution

  • I have done similar things with a two step process: part one is to create a web interface that provides start / stop commands. The web interface writes a file to the server. one for start, one for stop. start.lock, stop.lock.

    the second part is a bash script that looks for the presence of these files. Below is an example of the start.sh script that would check to start the service. It creates a a "running.lock" file to know not to try to start a running service. You cazn also use the files for reporting purposes.

    stop.sh and start.sh are cron scheduled to run frequently. For my purpose, they run every minute and the loop in the script is used to check more frequently. ie, each minute run and check 5 times once every ten seconds.

    I think with this you can modify to your specific needs. Hope it helps.

    #!/bin/bash
    
    for ((c=1; c<=5; c++))
    do
    
    file="start.lock"
    if [ -f "$file" ]
    then
            sfile="running.lock"
            if [ ! -f "$sfile" ]
            then
                    <command goes here>
                    touch running.lock
                    rm start.lock
            fi
    
    fi
    
    sleep 10
    done
    

    and to stop it:

    #!/bin/bash
    
    for (( c=1; c<=11; c++ ))
    do
    file="stop.lock"
    if [ -f "$file" ]
    then
            killall -9 <your service>
            rm running.lock
            rm start.lock
            rm stop.lock
    fi
    sleep 5
    done