cron jobs sort of ... writing a script to distribute
So I'm writing a script that must do certain things daily and I could set a cron job but I want to also share the script as a free download, and I want the script to just work for my users, no set-up, so I'm trying to find a way that, if the user is using Linux, or IIS or whatever that as long as they support PHP (or at least for most servers if I can't do it for all) the script will be able to execute a daily process on time each day.
Related Article: Cron - TERM environment variable not set
is a Web Development discussion thread by magicmarkuk that has 6 replies, was last updated 9 months ago and has been tagged with the keywords: cron, term.
Goldfinch
Junior Poster in Training
55 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Basically, I want to figure out a way to run any PHP code I want on a daily basis. I could just set up a cron job. Instead I want to be able to design a script that will trigger the script to be executed. The reason is that in order to provide it to users I want it wrapped up in a neat package ready for them to use.
Goldfinch
Junior Poster in Training
55 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
The purpose would be I wouldn't have to tell users to set-up a cron job as part of the install process, I think that will lower the technical challenge and increase the number of people who can use and like the script.
Goldfinch
Junior Poster in Training
55 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Gladly, I'll test this out and get back to you. If it works, I'll post details of the project I integrated it in so anyone can check out the code in an example and test it out.
Goldfinch
Junior Poster in Training
55 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
sure, I'll write up what i did in detail, because the period for my cron setting would be one day, I'll write a test script with a shorter period, it will still take some time after I finish writing it to make sure it goes through a few cycles without anamoly, but once I verify it works I'll post the details here - if it doesn't work, then at least we'll know sooner - if it fails I'll post details on errors it returns
Goldfinch
Junior Poster in Training
55 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Sorry, yesterday I didn't sleep well for some reason so I was nearly useless, but I was just beginning my test now and noticed your message. I'll get back to you.
Goldfinch
Junior Poster in Training
55 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Okay, so here's an update. Nothing yet. I've gotten a test designed that should tell me if its working or not. It's possible I didn't set it up correctly. Let me show you what I have:
4 files
testcron.php - your code, every time I run it I it prints the following in the window:
string(0) ""
<?php
$filename = "crontask.txt";
$output = shell_exec('crontab -l');
$something = file_put_contents($filename, $output.'* * * * * /home/frontand/public_html/comic/testrss.php'.PHP_EOL);
$cngDir = chdir('/home/frontand/public_html/comic/');
$exe = exec('crontab crontask.txt');
var_dump($exe);
?>
testrss.php - essentially the same approach as your file, it is the file to be executed
<?
$filename = "comic_test.txt";
$hello = date(DATE_ATOM);
$print = file_put_contents($filename, $hello.' - Hello World'.PHP_EOL);
?>
crontask.txt - currently its not exactly right - here's the output after several runs of your code:
* * * * * NEW_CRON
* * * * * /testrss.php
* * * * * /home/frontand/public_html/comic/testrss.php
* * * * * /home/frontand/public_html/comic/testrss.php
* * * * * /home/frontand/public_html/comic/testrss.php
finally the output file for the script to be executed, this script is designed to make it easy to test if the script is working
2012-11-29T21:38:19-05:00 - Hello World
So far the cron job hasn't worked but it's easily testible and it looks like you can run shell scripts, it might just be a permissions issue like you've said. I'm going to try chaning testcron.php permissions to 755.
Goldfinch
Junior Poster in Training
55 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
No, 755 didn't work directly, but another interesting error is that even if I delete the crontask file and create a new one it appears that the shell session isn't being closed so when I run the cron script it runs the new_cron line line even though its no longer part of the script as modified. If there is anything else you want me to test I'll take a try at it, just let me know.
Goldfinch
Junior Poster in Training
55 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Update, I think the issue just got weirder. I just discovered an e-mail I never used was filled with cron job records and cPanel said the cron jobs were running. What strange though, is that even though that would mean it worked, then why didn't my testing script work? If I ran my testing script manually it always successfully ran. Second Update: so yeah, its definitely a permissions issue. My e-mail said permission denied even though cPanel recognized that a valid request was received. So it looks like the permissions issue happens after the cronjob is properly scheduled.
Goldfinch
Junior Poster in Training
55 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0