#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    Italy
    Posts
    36
    Rep Power
    2

    How to launch a system command and then forget it


    I launch a program via exec() function. But the program causes my script sleep for a lot of time.

    I need to say to PHP: "launch this program, but then forget it. Don't wait until it works, what he does is none of your business". How can I do?
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,295
    Rep Power
    9400
    Potentially overkill but
    PHP Code:
    exec("nohup $command >/dev/null 2>/dev/null &"); 
    (you can redirect stdout and stderr wherever you want, of course)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Sep 2006
    Posts
    2,042
    Rep Power
    535
    Take special attention to the &.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    Italy
    Posts
    36
    Rep Power
    2
    Thank you both, the problem is that my script must run on all systems. Is there anything like /dev/null on Windows?
  8. #5
  9. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,295
    Rep Power
    9400
    nul, but the *nix and Windows ways of running commands in the background are very different so it doesn't matter.
    PHP Code:
    if (strncasecmp(PHP_OS"WIN"3) == 0) {
        
    exec("start /b $command");
    } else {
        
    exec("nohup $command >/dev/null 2>/dev/null &");

    You may need something more complex for the Windows command, depending on what you're running.

    Comments on this post

    • f_razzoli agrees
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    Italy
    Posts
    36
    Rep Power
    2
    It works. Thank you!
  12. #7
  13. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,555
    Rep Power
    2338
    If only there were documentation of these obscure functions, especially with user-contributed comments and snippets of code.

    Comments on this post

    • ManiacDan agrees
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!

IMN logo majestic logo threadwatch logo seochat tools logo