0

system("php ./somescript.php $something >/dev/null &");

I'm new to PHP and I am deciphering someone else's code and can't figure out what this code is doing. I looked at the system documentation here so I understand that it will execute the PHP script in somescript.php, and that $something is a parameter passed to that script...but after that I'm lost.

Also, what part of the script does $something get passed to? How does it know what function in somescript.php to call first?

hakre
  • 193,403
  • 52
  • 435
  • 836
richard
  • 12,263
  • 23
  • 95
  • 151
  • 1
    "execute the PHP script in somescript.php" after than nothing, that is what it does. want to know what "*somescript.php*" does - look in the file –  Aug 15 '12 at 20:11
  • So what is this "./dev/null &"? And what function in the PHP script does it run first?? – richard Aug 15 '12 at 20:11
  • Redirects any output to [/dev/null](http://en.wikipedia.org/wiki//dev/null). In other words, a black hole. – j08691 Aug 15 '12 at 20:11
  • http://stackoverflow.com/questions/8208033/what-does-dev-null-dev-null-at-the-end-of-a-command-do – Jeremy Harris Aug 15 '12 at 20:12
  • no crystal ball, what's in somescript.php ?? –  Aug 15 '12 at 20:12
  • `>/dev/null` means it will send the output of somescript.php to nowhere; `&` means it will run in the background (kind of unnecessary in this context). – Matt Aug 15 '12 at 20:12
  • @Matt I've been looking for something like that `&` for a long time. Do you know what it's called? – Waleed Khan Aug 15 '12 at 20:14
  • And to answer your question "How does it know what function in somescript.php to call first?": It executes the file like any other PHP-file, so top to bottom (of course with all functions calls etc. that come in-between). – insertusernamehere Aug 15 '12 at 20:15
  • 2
    @arxanas `&` spelled out is "ampersand". It used to be the last letter in the English alphabet; it is a shortened way of saying "and, per se, 'and'" (w, x, y, z, and, per se, "and"). – Matt Aug 15 '12 at 20:15
  • @Matt I meant in its context as a command :P. Searching for "ampersand" doesn't really help. – Waleed Khan Aug 15 '12 at 20:16
  • @Matt Not unnecessary, it's way to start daemon processes from PHP without blocking the execution of the current script. Now the code above may not actually work like this because it has not handled STDERR, and some linux flavours require you to do this as well. Personally when I'm doing this I like to do `$pid = (int) exec('nohup '.$cmd.' > /dev/null 2>&1 & echo $!');` – DaveRandom Aug 15 '12 at 20:17
  • 1
    @arxanas If you're wanting to execute commands in the background on *nix and completely detach them from the PHP script that started them, see my previous comment. If you want to do it on windows you have to do it via COM, and I don't know of a way to get the PID. – DaveRandom Aug 15 '12 at 20:21

4 Answers4

2

You are correct about it executing the script with $something passed as parameter. The > /dev/null is just piping the output into a black hole (i.e. suppressing it). The & part launches the script in the background.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
2
system("php ./somescript.php $something >/dev/null &");

can be split up as:

system(

This is the php function to execute a command in shell

system("php

We are asking the php command line interpreter to be run

system("php ./

we are asking the interpreter to look in current directory using ./

system("php ./somescript.php

we are saying that the php file to be executed in somescript.php

system("php ./somescript.php $something

we are passing $something which is a variable in the script running system() as an argument into the script somescript.php

system("php ./somescript.php $something >/dev/null

we are saying that the output should be sent to /dev/null ..which basically means do not print any output on the screen. > is the redirection command. So > /dev/null means redirect output to /dev/null which is like an alias for a "null device" or simply..nothing.

system("php ./somescript.php $something >/dev/null &");

Lastly we are asking the script to be run in the background using & which means as soon as the script starts, control is returned back to the shell

raidenace
  • 12,789
  • 1
  • 32
  • 35
1

It's basically running the ./somescript.php program, with $something as a command-line parameter.

It explicitly ignores the output by routing it to the null device.

And it runs it in its own shell and doesn't wait for it to finish (that's the & bit on the end).

I assume that somescript.php is a program that runs some kind of background task. It probably takes a bit of time to run, but your main program doesn't need to know about the results of that task, so it doesn't need to wait for it to finish.

[EDIT]

$something is passed into the program as a command-line argument. When PHP is called from the command line, the arguments are populated into the $argv array.

See the PHP manual for more info:

Spudley
  • 166,037
  • 39
  • 233
  • 307
1

1 - system: execution of an external program from PHP
2- php ./somescript.php $something: PHP cli execution of a script called ./somescript.php with one argument being $something.
3 - >/dev/null: redirects stdout to /dev/null which means you won't see standard output, but you will still see stderr. More info on output redirections. That's pure UNIX nothing to do with PHP.
4 - &: sends command in the background so that PHP execution can continue without having to wait for the external program to complete. Once again, pure UNIX, nothing to do with PHP per se. More info.

Community
  • 1
  • 1
Max
  • 12,794
  • 30
  • 90
  • 142