Kill command does the job but gives error
As @Bodo pointed out, there was more than one PIDs and only one of them got killed while the other showed error as it wasn't started by the same user.
I changed the grep command to filter the process started by the same user and then killed it, so it worked.
Update:
I used this command to successfully run my code and terminate the app as needed.
ps -u | grep service.py | grep -v grep | awk '{print $2}' | xargs kill -9
How to kill all subprocesses of shell?
After starting each child process, you can get its id with
ID=$!
Then you can use the stored PIDs to find and kill all grandchild etc. processes as described here or here.
Bash - starting and killing processes
1,2. Use pgrep
. I don't remember off the top of my head whether pgrep
has -c
parameter, so you might need to pipe that to wc -l
.
3: that output is produced by your shell's job control. I think if you run that as a script (not in an interactive shell), there shouldn't be such an output. For an interactive shell, the are number of ways to turn that off, but they are shell-dependent, so refer to your shell's manual.
Kill process by name in ubuntu
You can use killall php
. But note that it kills all PHP processes, not just those running script.php.
Bash: Killing all processes in subprocess
You can set a trap in the subshell to kill any active jobs before exiting:
(trap 'kill $(jobs -p)' EXIT; sleep 5; echo done ) & pid=$!
Related Topics
How to Get Output of Proc_Open()
JavaScript Equivalent of PHP's List()
Undefined Variable Problem with PHP Function
Setting Environment Variables for Accessing in PHP When Using Apache
.Htaccess Rewriterule to Preserve Get Url Parameters
Caching Http Responses When They Are Dynamically Created by PHP
How to Use Pdo's Prepared Statement for Order by and Limit Clauses
Check Whether or Not a Cidr Subnet Contains an Ip Address
Package 'Php5-Gd' Has No Installation Candidate
Best Way to Get Files from a Dir Filtered by Certain Extension in PHP
Laravel Stylesheets and JavaScript Don't Load for Non-Base Routes
How to Access the PHP.Ini File in Godaddy Shared Hosting Linux
How to Use Curl Instead of File_Get_Contents