The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

how to schedule cron outside of cpanel? Via PHP?

Discussion in 'General Discussion' started by dlevens, Jul 13, 2004.

  1. dlevens

    dlevens Member

    Joined:
    Jun 20, 2004
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    1
    I am trying to find an easy way to run a cron job without having to login to cpanel click on cron and manually create it, then schedule it to run every minute, then delete it after it runs.

    I have a few scripts that I need to run once in awhile, but not on a consistant enough basis to put them on a schedule. I would like to have a way to just run something that would create the cron job and then after it runs delete it self.

    Maybe a php file?

    It would be nice if cpanel would add a config option where you did not need to schedule a cron but could instead just click a button to run it once. At least then I could just login real quick and click run once.

    Any help would be appreciated,
    Dennis
     
  2. chirpy

    chirpy Well-Known Member

    Joined:
    Jun 15, 2002
    Messages:
    13,475
    Likes Received:
    20
    Trophy Points:
    38
    Location:
    Go on, have a guess
    Well, that's not what CRON is for, since it's a scheduling application.

    I presume that you don't have shell access? If you do, then use that. If not, why not write a short script in PHP or perl that you can browse to that runs the system command that you would have put in your CRON job. Be sure to put anything like that inside a password protected directory.
     
  3. cPDan

    cPDan cPanel Staff
    Staff Member

    Joined:
    Mar 9, 2004
    Messages:
    711
    Likes Received:
    3
    Trophy Points:
    18
    Or if you don't want to create a cron job at all for a simple run just sleep().

    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    for(1..20) {
       print "Do your stuff here";
       sleep(60);
    }
    
    That will run the same code every 60 seconds 20 times (IE it will run for 20 minutes)

    No muss no fuss :)

    I'm sure PHP has a similar sleep function if you're forced to use PHP...
     
  4. dlevens

    dlevens Member

    Joined:
    Jun 20, 2004
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    1
    Hey guys great suggestions. You are right I do not have shell access and also cannot use phpshell or anything that tries to give me shell access. I did try some php code but found that I cannot use sudo and had issues with permissions when running using public (nobody).

    This is a sample of a ham.sh script I run as needed once I get all my spam in the spam folder and ham in the ham folders I run a modified version of this:


    The only difference is it goes on 15 more times repeating for FILE in ... and I type in all 15 folders I keep my ham in.

    Currently I have to log into cpanel, then make a cron to run every minute to run this and once it runs once I delete the cron.


    I wish I could just browse to a protected website and click a button called run ham and another run spam that would do all this for me.

    I was playing with this code:

    But this was not working because the process was not running from cron so it did not have enough permissions to work. Since it has to read many files and write to many files.

    Dennis
     
  5. Big Gorilla

    Big Gorilla Active Member

    Joined:
    Jan 30, 2004
    Messages:
    34
    Likes Received:
    0
    Trophy Points:
    6
    If your host has suExec enabled (yours does) you can run it as a CGI so it will inherit your user id.

    Put "#!/usr/bin/php" as the first line of the script, and include your PHP code, make sure it's permissions are executable (755) and it's in your cgi-bin, or a cgi executable directory.
     
  6. dlevens

    dlevens Member

    Joined:
    Jun 20, 2004
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    1
    SWEET

    It worked! I modified the code to the following:

    sa.php
    Now I just run it by a url. I assume I need to place the sa.php in the cgi-bin directory? Or would it run other locations? Now I just need to modify the squirrelmail code to add this url right inside my sqirrelmail so once I get my ham and spam in the right folders I just click once and have sa-learn do its thing.

    Thanks for the info, this is just what I needed
    Dennis
     
    #6 dlevens, Jul 14, 2004
    Last edited: Jul 14, 2004
Loading...

Share This Page