Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

positonic's avatar

Called php exec from scheduled jobs and console commands

I have console command that I can run manually and it will start up some pm2 node processes for me.

I can't seem to find a way to do this automatically though, as the exec command doesn't seem to work when run in a job (my first idea), and suprisingly it also doesn't work if I add my already working console job from to the console kernal so that it is scheduled.

This seems to be the same problem(https://laracasts.com/discuss/channels/laravel/exec-in-queued-job) , but I can't get exec_shell working. Anyone have any ideas?

0 likes
3 replies
lostdreamer_nl's avatar

Do you want those processes to run in the background (no output back to PHP), or should they block the PHP process while they are running (and give response back to PHP)?

positonic's avatar

@lostdreamer_nl the logic is: For each relevant user:

exec('node command to check if there's a node socket running for the user')

if not.

exec('pm2 launch a node socket')

So both are blocking... no comms needed back other than success....

lostdreamer_nl's avatar

Lets first see if it's allowed to run any process:

$output = [];
exec ( "ls -al", $output);
\Log::debug('output of ls: ', $output);

If you put this in an artisan command, and run that command, do you see any output in your laravel.log file ? (it should get a directory listing )

If not, then php is probably setup to not be able to run commands (running in safe mode), if it does work, then we'll try it with:

$cmd = "pm2 launch a node socket &> /dev/null &";
exec('/bin/bash -c "' . addslashes($cmd) . '"');

It should spawn the process $cmd in the background, so after running your artisan command it should immediately return to your console.

You can check if the command is actually running with the "top" command / "ps aux"

Please or to participate in this conversation.