Jump to content

Recommended Posts

I'm trying to call a script to run in the background. The script is in a file called script1.php, and I'm calling it from a test page through the following code:

 

<?
exec("/usr/bin/php /home/myaccount/public_html/test/script1.php >/dev/null &");
?>

 

I took this code from someone on the internet. They claimed this is the way to do it.

However nothing happens. The script is supposed to write in the database.

If I run script1.php directly, it works fine.

 

Is the syntax I'm using correct?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/265140-run-background-script/
Share on other sites

There's nothing wrong with the syntax. All you're doing is using the php binaries to execute the script, writing the output to nothingness (">/dev/null") and then telling the command to run in the background ("&"). I'm guessing there's a permissions issue or something along those lines, so I would add in the second parameter and remove the write to /dev/null so you can check the response.

You should redirect the stderr stream to dev null as well, using a line like this:

exec("/usr/bin/php /home/myaccount/public_html/test/script1.php >/dev/null 2>&1 &");

 

Beyond that, make sure you have the correct path to your script1.php file, and also to the php executable.  If you have SSH access to the server you can run the command which php to find out the path to the PHP binary.

 

 

 

Are you on a shared hosting system?  I'm facing the same problem and have been pounding my head on the desk trying to find the magic combination.  Unfortunately, I have just been informed by Myhosting.com that they are blocking "exec" on shared hosting servers.  I'll need to move up to a VPS system in order to gain that functionality.  The annoying thing is that I used "function_exists" to convince myself that the function was available.  So, just because it's on the system, doesn't mean you can use it...

Are you on a shared hosting system?  I'm facing the same problem and have been pounding my head on the desk trying to find the magic combination.  Unfortunately, I have just been informed by Myhosting.com that they are blocking "exec" on shared hosting servers.  I'll need to move up to a VPS system in order to gain that functionality.  The annoying thing is that I used "function_exists" to convince myself that the function was available.  So, just because it's on the system, doesn't mean you can use it...

 

That's pretty much standard on a shared host.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.