1

I have a webpage that it should run some jobs as php processes in background. Also, it should be able to identify each process to close it later. Ex. Worker1, Worker2... How is this achieved? also how to kill the procesees? The OS is ubuntu. *Those scripts are always running in the background, so they don't get killed by themselves.

2
  • 4
    Welcome to Stack Overflow, @Soheil Yahyaee. I know this is probably not what you wanted to see, but here we like to see what you have tried first and then we can jump in and help you. Give it a try your self, post some code and tell us where you're getting stuck. Commented Sep 25, 2015 at 14:31
  • What have you tried so far? Where are you stuck? Why not run them like every other script? Commented Jun 18, 2021 at 7:29

1 Answer 1

3

You can put scripts and other shell tasks in background using nohup at the beginning and the & symbol at the end of the command:

~$ nohup php script.php >> /var/tmp/script.log 2>&1 &

Note that with the option 2>&1 you redirects the output (standard error and output) to the standard output, then to a file for logging (here /var/tmp/script.log).

EDIT: with the command jobs you can list the process you have active into your session (here 1797 is the process pid):

~$ jobs -l
[1]+  1797 Running   nohup php script.php >> /var/tmp/script.log 2>&1 &

You can send signals to the process, after you discover the process pid. To kill "nicely" a process (where ${PID} is the process pid):

~$ kill -SIGTERM ${PID}

If the process is stuck you could use the signal SIGKILL (or -9). Note that SIGKILL cannot be intercepted, then the process ends immediately without any "cleaning" operations (closing temporary files, etc). kill -9 ${PID} or kill -SIGKILL ${PID} should be used only as last resource.

Here some theory:

Input/Output redirection:

Some courses to become GNU/linux system administrators:

Sign up to request clarification or add additional context in comments.

4 Comments

multiple instances of script.php should run and each of them has for example a username. how to kill them later?
maybe a little help about the "2>&1" section? what does it do?
with 2>&1 you can redirect the standard error on the standard output. The answer is modified. A link about redirection: tldp.org/LDP/abs/html/io-redirection.html
be careful in sending signal to processes: you can put them in a state of instability.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.