Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9066039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:39:04+00:00 2026-06-16T16:39:04+00:00

I have a long running PHP script that i want to be executed in

  • 0

I have a long running PHP script that i want to be executed in background on server after a user action. and the user should be redirected to other page while command should be running in background.
Below is the code

$command = exec('php -q /mylongrunningscript.php');
header("Location: /main.php?action=welcome");

The above script is running fine, but page does not redirected until $command = exec('php -q /mylongrunningscript.php'); is executed.

I want that user should be immediately redirected to the welcome page.

Is there any other way to achieve this task.
The other idea is that that $command = exec(‘php -q /mylongrunningscript.php’); should be executed on welcome page, but welcome page HTML is shown after the command has executed. command takes about 5,6 minutes and this time page does not redirects.

I am On Cent os Linux with PHP 5.3

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:39:05+00:00Added an answer on June 16, 2026 at 4:39 pm

    Can you try this instead:

    $result = shell_exec('php -q /mylongrunningscript.php > /dev/null 2>&1 &');
    

    PS: Note that this is redirecting stdout and stderr to /dev/null If you want to capture output then use:

    $result = shell_exec('php -q /mylongrunningscript.php > /tmp/script.our 2>&1 &');
    

    Alternatively use this PHP function to run any Unix command in background:

    //Run linux command in background and return the PID created by the OS
    function run_in_background($Command, $Priority = 0) {
        if($Priority)
            $PID = shell_exec("nohup nice -n $Priority $Command > /dev/null & echo $!");
        else
            $PID = shell_exec("nohup $Command > /dev/null & echo $!");
        return($PID);
    }
    

    Courtesy: A comment posted on http://php.net/manual/en/function.shell-exec.php

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a long running PHP script that has a memory leak which causes
I have a PHP script running that's taking hours, it usually takes a long
I have a PHP script running on a cron that can take a long
I have an Apache server running a PHP script that receives data via a
I have a long running php script which is basically an infinite loop listening
I have a very long running script that is doing a pretty significant amount
I have a long-running script that seems to occasionally report the following NOTICE-level error:
I have a long running PHP process, that sometimes hangs in a loop. This
I have some long-running CLI PHP scripts that run regularly via cron. I'd like
I have a PHP script that takes a long time (5-30 minutes) to complete.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.