0

I'm trying to terminate a shell script using PHP code.

I have created a shell script foo.sh which calls a PHP file

#! /bin/bash

cd /var/www/html/test
php test.php

Following is test.php

<?php
  exec('exit 0');
?>

For some reason the shell script is not exiting.

1
  • Wil test.php take a long time to run? Commented Nov 25, 2013 at 6:33

1 Answer 1

1

Your exit(0) in PHP will terminate process with PHP itself, and not parent process (if any). To terminate your bash script, you will need to find it's pid via ps and grep, or, alternatively, use killall:

system('killall foo.sh');

-in PHP

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

4 Comments

if PID of foo.sh is 2710 my code should be like system('killall 2710'); or system('killall foo.sh');?
killall takes prosess name - and so there's no sense to pass pid there. To say more, your pid will be new each time, while killall is intended to kill process independent of it's pid
However, the proper way to do this is to cooperate with the caller. For example, php test.php || exit $? will exit with the exit code of the PHP script if it's non-zero, and continue otherwise.
I agree that it's normal way, while OP's question may be more common (for example, he has not control over parent script)

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.