how to set close-on-exec by default

No and no. You simply need to be careful and set close-on-exec on all file descriptors you care about. Setting it is easy, though: #include <fcntl.h> fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); #include <unistd.h> /* please don’t do this */ for (i = getdtablesize(); i –> 3;) { if ((flags = fcntl(i, F_GETFD)) != -1) … Read more

php in background exec() function

exec() will block until the process you’re exec’ing has completed – in otherwords, you’re basically running your ‘test.php’ as a subroutine. At bare minimum you need to add a & to the command line arguments, which would put that exec()’d process into the background: exec(“php test.php {$test[‘id’]} &”);

Running Python code contained in a string

You can use the eval(string) method to do this. Definition eval(code, globals=None, locals=None) The code is just standard Python code – this means that it still needs to be properly indented. The globals can have a custom __builtins__ defined, which could be useful for security purposes. Example eval(“print(‘Hello’)”) Would print hello to the console. You … Read more

Go exec.Command() – run command which contains pipe

Passing everything to bash works, but here’s a more idiomatic way of doing it. package main import ( “fmt” “os/exec” ) func main() { grep := exec.Command(“grep”, “redis”) ps := exec.Command(“ps”, “cax”) // Get ps’s stdout and attach it to grep’s stdin. pipe, _ := ps.StdoutPipe() defer pipe.Close() grep.Stdin = pipe // Run ps first. … Read more

What’s the difference between escapeshellarg and escapeshellcmd?

Generally, you’ll want to use escapeshellarg, making a single argument to a shell command safe. Here’s why: Suppose you need to get a list of files in a directory. You come up with the following: $path=”path/to/directory”; // From user input $files = shell_exec(‘ls ‘.$path); // Executes `ls path/to/directory` (This is a bad way of doing … Read more

PHP exec() not returning error message in output

You need to capture the stderr too. Redirecting stderr to stdout should do the trick. Append 2>&1 to the end of your command. e.g. exec(“/usr/bin/svn –username something –password something –non-interactive log -r HEAD –xml –verbose http://a51.unfuddle.com/svn/a51_activecollab/ 2>&1”, $output); If it is a complex command (e.g. one with a pipe, like doing a mysqldump and passing … Read more