如何编写'shutdown -r 1'脚本并返回退出状态?

I am writing a program that will at some point call a shell script. I need this shell script (bash, or if necessary PHP 4+ will work) to be called by the program, and return an exit status that I can relay before the 1 minute is reached and the system reboots.

Here's an idea of what I mean, best as I can describe:

  • Program calls 'reboot' script
  • Reboot script runs 'shutdown -r 1' and then exits with a status of 0
  • Program echo's out the exit status
  • Server reboots

I can get everything to work except the exit status - no matter what I try the program never exits its loop waiting for an exit status, so it never returns anything but the reboot still occurs. This program runs other scripts that return exit statuses, so I need this one to as well to maintain functionality and all that...

Any help is appreciated!

EDIT- The program that calls the reboot script is a PHP script that runs in a loop. When certain events happen, the program runs certain scripts and echos out the exit status. All of them work but this - it never returns an exit status.

Scripts are being called using system($cmd) where $cmd is './scriptname.sh'

Assuming you're opening the process using proc_open, then calling proc_get_status should return an array that has the exit code in it.

You could create a bash script that backgrounds the shutdown process:

#!/bin/bash
shutdown -r 1 &
exit 0

This returns control to the parent shell, which receives "0" as the exit code.

Unfortunately, you can't rely on PHP's system() and exec() functions to retrieve the proper return value, but with a nice little workaround in BASH, it's possible to parse exit code really effectively:

function runthis($command) {
  $output = array();
  $retcode = -1;
  $command .= " &2>1; echo $?";

  exec($command, $output, $retcode);
  $retcode = intval(array_pop($output));

  return $retcode;
}

if (runthis("shutdown -r 1") !== 0) echo "Command failed!
";

Let me break down what does the code doing:

  • $command .= " &2>1; echo $?"; - expand the command so we pipe the stderr into stdout, then run echo $?
  • echo $? - this special bash parameter which expands to the last executed command's exit code.
  • exec($command, $output, $retcode); - execute the command. ($retcode is just a placeholder here since the returned data isn't trustworthy. We'll overwrite it later.) The command's output will be written in $output as an array. Every element will represent an individual row.
  • $retcode = intval(array_pop($output)); - parse the last row as an integer. (since the last command will be echo $?, it will be always the actual exitcode.

And that's all you need! Although it's a really crude code, and prone to errors if not used correctly, it's perfect for executing simpler tasks, and it will always give you the proper exit code.

For more professional (and programmatic) approach, you have to dig yourself into PHP's pnctl, posix, stream functions, and also Linux pipe handling.