Title: function execute background
Last modified: July 26, 2021

---

# function execute background

 *  [tharindu1995](https://wordpress.org/support/users/tharindu1995/)
 * (@tharindu1995)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/function-execute-background/)
 * Hi Everyone,
    I’m create a plugin to get woocommerrce data to external system.
   but it take lot of time to execute also serve show 502 error after i used cron
   function to execute that function but it not working properly
 *     ```
       add_action('wpse71941_cron', 'wpse71941_long_running');
       function wpse71941_long_running($args)
       {
   
           // might need to call <code>set_time_limit</code> here
           set_time_limit(0);
   
           include 'customers.php';
               include 'orders.php';
               include 'products.php';
   
           // return normal time limit
           if($l = ini_get('max_execution_time'))
               set_time_limit($l);
       }
   
       // schedule the event for right now
       wp_schedule_single_event(
           time(), 
           'wpse71941_cron', 
           array('args' => 'for', 'callback' => 'function')
       );
       ```
   
 * this cron execute repeatly i want to execute it a only one time like execute 
   with plugin activate after run my other stuff. how can i do it

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Marcel Overrödder](https://wordpress.org/support/users/dufmov/)
 * (@dufmov)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/function-execute-background/#post-14702113)
 * Cron’s purpose is re-running commands at certain intervals. If you want to run
   code only once and only in a specific scenario within wordpress, that’s a sign
   you should actually be looking for a hook.
 * To run a function only once after activation of a specific plugin of yours, you
   probably want to use the ‘register_activation’ hook within that plugin:
    [https://developer.wordpress.org/reference/functions/register_activation_hook/](https://developer.wordpress.org/reference/functions/register_activation_hook/)
 * If it’s not your plugin and you can’t/shouldn’t just add code to it, this either
   doesn’t apply or there’s another hook for that.
    -  This reply was modified 4 years, 10 months ago by [Marcel Overrödder](https://wordpress.org/support/users/dufmov/).
 *  Thread Starter [tharindu1995](https://wordpress.org/support/users/tharindu1995/)
 * (@tharindu1995)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/function-execute-background/#post-14702506)
 * [@dufmov](https://wordpress.org/support/users/dufmov/)
    Thank you for your reply.
   that’s function working fine but server execution time respond a error 502 “Apache-
   Long-Process-Killer” i want to run my functions background after activated plugin.
   do you have any idea to do that

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘function execute background’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 2 participants
 * Last reply from: [tharindu1995](https://wordpress.org/support/users/tharindu1995/)
 * Last activity: [4 years, 10 months ago](https://wordpress.org/support/topic/function-execute-background/#post-14702506)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
