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

motinska94's avatar

exec() and shell_exec() Returns null on both 'exe' and 'py' files

I wrote a simple python script to run once a day and rescale-resize-optimize user pictures to optimize loadtimes. I tried to run it using exec() and shell_exec() but every single time it returns either "", [] or null.

My main.py and resize.exe files do all the work (they're same script in different formats) and I put them both in the same dir as images. Using chmod might be unnecessary, but it was the last thing I tried before running out of patience and posting here.

My Code :

			chmod(public_path('\images\users\main.py'), 777);
            $response = shell_exec('python ' . public_path('\images\users\main.py'));
            dd($response);

returns null

	chmod(public_path('\images\users\main.py'), 777);
    $response = exec('python ' . public_path('\images\users\main.py'));
    dd($response);

returns ""

    chmod(public_path('\images\users\resize.exe'), 777);
    $response = exec(public_path('\images\users\resize.exe'));
    dd($response);

returns ""

I tried every combination of files and commands, with and without chmod, even tried adding sleep(2) because I thought laravel wasn't waiting for my script to finish its job (which takes less than a second).

0 likes
13 replies
newbie360's avatar

@motinska94 you may try debug like this

if (! exec('python ' . public_path('\images\users\main.py'))) {
	dd('has error');
}
motinska94's avatar

@newbie360 it returns has error, strangely when I dd out public_path('\images\users\main.py') and put the exact output in cmd, it views the python file on vscode.

motinska94's avatar

@Sinnbeck I saw this a lot while I was googling but I didn't think I need to install a whole package to run a simple exe or py file once a day, but it seems it's the last thing I haven't tried. Thanks!

motinska94's avatar

Unbelievable. It's still not working.

Tried this :

$process = new Process(['python ' . public_path() . '\images\users\main.py']);
            $process->run();
            if (!$process->isSuccessful()) {
                dd($process->getErrorOutput());
            }

Output :

Filename, directory name, or volume label syntax error.

I checked names and paths like 100 times in the past 4 hours. Same exact code I gave to process is running just fine on CMD. Is there a security prevention or something I need to disable on config files or somewhere? I'm literally out of ideas.

Stackoverflow people can somehow achieve the same thing with something as simple as exec($path . $filename)

motinska94's avatar

@newbie360 I tried that a million times. It's not that either. Process seems to be working but neither exe nor py files are working correctly. They both work perfectly when I double click or run them through my terminal.

Well, I'm assuming that Process is working because this line

dd($process->getLastOutputTime());

outputs this : 1671143406.0232 And the docs of the Process package says it'll return null if the project didn't run.

motinska94's avatar

@newbie360 Thanks! I'll look it up first thing tomorrow. I'm so done for today.

Edit : It's not as good as my script but it still works with resizing images and keeping the aspect ratio, so I'll take that 😅

Sinnbeck's avatar

@motinska94 are you on Linux? Cause exec should work as expected there. If you are on windows, then chances are you will run into problems. But the process lib should fix that. I have once written a docker manager in php that worked on all OSs. And process is already installed. Laravel will even add first party support for it soon

1 like
motinska94's avatar

@Sinnbeck I installed Intervention Image package and seems to work decently for now. Maybe I'll schedule my exe file to run on the server when project goes live. Thanks so much.

Please or to participate in this conversation.