PHP script as a cron job

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pol

    #1

    PHP script as a cron job

    Can php scripts be initiated as a cron jobs running indefinately in a
    loop performing a task?

    Any recommendations for writing a php script that will run as an unix
    server process that will run indefinitely once it starts?

    If cron starts it every 10 minutes. How can I check if I my process is
    running and then end if an instance is already running?

    Thanks
  • Jonathan Wiltshire

    #2
    Re: PHP script as a cron job

    "Pol" <Pol@nospam.com > wrote:
    [color=blue]
    > Can php scripts be initiated as a cron jobs running indefinately in a loop
    > performing a task?
    >
    > Any recommendations for writing a php script that will run as an unix
    > server process that will run indefinitely once it starts?[/color]

    Yes. Ensure safe mode is off, and then you can use set_time_limit( ) to 0 to
    disable
    script killing. Call "php scriptname.php" from cron to launch it.
    [color=blue]
    > If cron starts it every 10 minutes. How can I check if I my process is
    > running and then end if an instance is already running?[/color]

    If it's a server, why not try connecting to it and see if it times out? Then
    you'd know
    if it's responding or not. If you're happy with the command line, and safe
    mode is off,
    you can use exec() to run shell commands directly to kill it.

    Jon


    Comment

    • s

      #3
      Re: PHP script as a cron job

      On Thu, 02 Mar 2006 15:18:43 -0500, Pol <Pol@nospam.com > wrote:
      [color=blue]
      >Can php scripts be initiated as a cron jobs running indefinately in a
      >loop performing a task?
      >
      >Any recommendations for writing a php script that will run as an unix
      >server process that will run indefinitely once it starts?
      >
      >If cron starts it every 10 minutes. How can I check if I my process is
      >running and then end if an instance is already running?[/color]

      One approach is to have your script write a pid file, and check for
      the presence of pid files from other instances of itself.

      There are a few different ways you can use the pid file; here is some
      code from a daemon that I have. This code writes a pid file for this
      instance, then goes looking to see what other pid files exist. If the
      create time of any given pid file is more than 12 hours old, the old
      process is killed.

      My goal here was to ensure that no running instance had been alive for
      more than 12 hours (I had my reasons). But you could easily rework the
      logic to, for example, prevent multiple instances from running at the
      same time.

      <?
      #Write a pid file
      touch('pidfiles/' . getmypid());

      #Look for other processes
      $dir = opendir('pidfil es') or die("Can't open PID directory\n");
      while(($filenam e = readdir($dir)) !== false){
      if(($filename == '.') || ($filename == '..')){
      continue;
      }
      $time = filectime('pidf iles/' . $filename);
      if($time < (time() - 43200)){
      #Kill off the old process
      `kill -9 $filename`;
      unlink('pidfile s/' . $filename);
      }
      }

      // Do your daemon stuff here...

      #Remove our own pid file
      unlink('pidfile s/' . getmypid());
      ?>

      If you use this approach, don't forget that your script should remove
      its own pid file if/when it finishes running cleanly, and it should
      remove the pid file of any other process that it kills off.

      hth

      --
      <s@guerril.la.a pe> (to email, remove .ape)
      Apologies for the ad below.
      --

      *** Free account sponsored by SecureIX.com ***
      *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***

      Comment

      • David Haynes

        #4
        Re: PHP script as a cron job

        s wrote:[color=blue]
        > On Thu, 02 Mar 2006 15:18:43 -0500, Pol <Pol@nospam.com > wrote:
        >[color=green]
        >> Can php scripts be initiated as a cron jobs running indefinately in a
        >> loop performing a task?
        >>
        >> Any recommendations for writing a php script that will run as an unix
        >> server process that will run indefinitely once it starts?
        >>
        >> If cron starts it every 10 minutes. How can I check if I my process is
        >> running and then end if an instance is already running?[/color]
        >
        > One approach is to have your script write a pid file, and check for
        > the presence of pid files from other instances of itself.
        >
        > There are a few different ways you can use the pid file; here is some
        > code from a daemon that I have. This code writes a pid file for this
        > instance, then goes looking to see what other pid files exist. If the
        > create time of any given pid file is more than 12 hours old, the old
        > process is killed.
        >
        > My goal here was to ensure that no running instance had been alive for
        > more than 12 hours (I had my reasons). But you could easily rework the
        > logic to, for example, prevent multiple instances from running at the
        > same time.
        >
        > <?
        > #Write a pid file
        > touch('pidfiles/' . getmypid());
        >
        > #Look for other processes
        > $dir = opendir('pidfil es') or die("Can't open PID directory\n");
        > while(($filenam e = readdir($dir)) !== false){
        > if(($filename == '.') || ($filename == '..')){
        > continue;
        > }
        > $time = filectime('pidf iles/' . $filename);
        > if($time < (time() - 43200)){
        > #Kill off the old process
        > `kill -9 $filename`;
        > unlink('pidfile s/' . $filename);
        > }
        > }
        >
        > // Do your daemon stuff here...
        >
        > #Remove our own pid file
        > unlink('pidfile s/' . getmypid());
        > ?>
        >
        > If you use this approach, don't forget that your script should remove
        > its own pid file if/when it finishes running cleanly, and it should
        > remove the pid file of any other process that it kills off.
        >
        > hth
        >
        > --
        > <s@guerril.la.a pe> (to email, remove .ape)
        > Apologies for the ad below.
        > --
        >
        > *** Free account sponsored by SecureIX.com ***
        > *** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***[/color]


        flock a file in /tmp and fail if you don't get the flock.
        Since the lock is handled by the kernel, it will die if the locking
        process dies.

        -david-

        Comment

        Working...