I am trying to run bash scripts on PHP asynchronous, my current scripts waits until first script is executed before it starts to run the next one:
for ($i = 0; $i < 10; $i++) {
exec('php /var/www/index.php $(nmap -p 80 google.com)');
}
I found out to run async, i must put '&' at the end of my script to run it in foreground:
for ($i = 0; $i < 10; $i++) {
exec('php /var/www/index.php $(nmap -p 80 google.com) &');
}
This script will pass result of $(nmap -p 80 google.com) to my index.php and run itself in foreground. Problem is - i need to cancel that process once the script is finshed. I tried doing it like so:
for ($i = 0; $i < 10; $i++) {
exec('php /var/www/index.php $(nmap -p 80 google.com) $BASHPID &');
}
I pass a second parameter to index.php ($BASHPID) - process id of that bash script, then inside that index.php file, i run another exec command to kill that process. Problem is - it passes wrong process ID, and it doesn't kill the process in the end.
Any suggestions?