dannyd Posted February 21, 2008 Share Posted February 21, 2008 I created a php script copylogs.php How do I go about implementing it as a cron script ? how do I located the cron file to add my cron job what line would I add to have it run once an hour or once a day ? Quote Link to comment https://forums.phpfreaks.com/topic/92351-cron-job/ Share on other sites More sharing options...
rhodesa Posted February 21, 2008 Share Posted February 21, 2008 Is this your system or on a hosting service somewhere? If it's on a hosting service, you need to look through their FAQ or contact them, because every place has a different process. Quote Link to comment https://forums.phpfreaks.com/topic/92351-cron-job/#findComment-473175 Share on other sites More sharing options...
dannyd Posted February 25, 2008 Author Share Posted February 25, 2008 I have root access to the server. I can edit the cronjobs running (crontab -e) What line do I put if I want the cronjob to run once a day at midnight ? Quote Link to comment https://forums.phpfreaks.com/topic/92351-cron-job/#findComment-475825 Share on other sites More sharing options...
rhodesa Posted February 25, 2008 Share Posted February 25, 2008 Here is a full description on how to use cronjobs: http://www.hmug.org/man/5/crontab.php There are 2 parts to a cronjob: when and what. -The when is easy. It will be '0 0 * * *'. That is every day at midnight. -The what is not always so easy, as not every PHP install is the same. Basically we want to test running it from the command line first. You can read about all of the PHP switches with typing 'php -h' on the command line. But, it should end up looking something like this: /usr/bin/php -f /full/path/to/script.php Make sure you use the full path to both the PHP executable and the PHP script. If you are unsure of the path to the PHP executable, type 'which php' and it should tell you. Putting the above together, your cronjob should look like this: 0 0 * * * /usr/bin/php -f /full/path/to/script.php REMEMBER: It will run the job as whatever user you are logged in as. So be careful if you are logged in as a privileged user. Hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/92351-cron-job/#findComment-475927 Share on other sites More sharing options...
dannyd Posted February 25, 2008 Author Share Posted February 25, 2008 Thanks, I want to run it everday at 10:30am .. is this right: 30 10 * * * /path/to-my-script/script.php I'm a little hesitant with this. Quote Link to comment https://forums.phpfreaks.com/topic/92351-cron-job/#findComment-476018 Share on other sites More sharing options...
rhodesa Posted February 25, 2008 Share Posted February 25, 2008 Part one is correct. It will run the command every day at 10:30am Part two *may* work. There are 2 ways to execute a PHP script from the command line: -One way (the way you just posted) is to execute the script directly. This requires that the PHP compiler is in the systems PATH environmental variable. It also requires that you have the following code as the first line of the script: #!/usr/bin/php To the operating system, your script is just a text file. It doesn't know what do with it. This line tells the system to use the PHP compiler to run the script. Finally, you must make sure the script has executable permission. You can do this by running the following command: chmod +x test -The other way (which is the way I posted before and my preferred way) is to execute the PHP compiler directly and pass your script as an argument to it. This does not need any of the previous options requirements. Either way, whatever you choose to be part two of the cronjob, it needs to work when execute it directly on the command line. So try executing it that way first, make sure it works, then use it in your cronjob. Both ways are discussed here: http://us.php.net/features.commandline Quote Link to comment https://forums.phpfreaks.com/topic/92351-cron-job/#findComment-476072 Share on other sites More sharing options...
dannyd Posted March 6, 2008 Author Share Posted March 6, 2008 In my cron log im getting this error: Mar 6 11:57:01 intra1 crond[2286]: (tmp.XXXXGHJVjz) ORPHAN (no passwd entry) Mar 6 11:57:01 intra1 crond[2286]: (root) RELOAD (cron/root) Mar 6 11:57:01 intra1 crond[2286]: (tmp.XXXXFqiL6E) ORPHAN (no passwd entry) Im guessing the cron isnt running as root. How can i specify a cron job to run as the root user ? Quote Link to comment https://forums.phpfreaks.com/topic/92351-cron-job/#findComment-485070 Share on other sites More sharing options...
steviewdr Posted March 7, 2008 Share Posted March 7, 2008 To run a cronjob as root (or any user), put your cronjob as a file in: /etc/cron.d/nameofjob -steve Quote Link to comment https://forums.phpfreaks.com/topic/92351-cron-job/#findComment-485704 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.