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.

Recommended Answers

All 15 Replies

I don't get it?

What kind of cron job is it? Are you running a specific file on YOUR server that you want other people to be able to have on their server and execute YOUR FILE from their server?

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.

I think the only way to do this is using cronjobs, since the PHP has to be executed. I don't think there is anyway to make a PHP script that runs itself.. I don't get what the purpose of this would be anyway?

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 - Ok, I have a Linux box and I'll connect to it and try and write a script for you to do this. It might not pay off and someone could come up with an answer before me, but, hopefully, it will work.

commented: thank you, that's very kind +1

I think this code will work:

<?php

        $filename = "../../tmp/crontask.txt";
        $output = shell_exec('crontab -l');
        $something = file_put_contents($filename, $output.'* * * * * NEW_CRON'.PHP_EOL);
        $cngDir = chdir('../../tmp/');
        $exe = exec('crontab crontask.txt');
        var_dump($exe);
 ?>

There is a lot of issues around permissions though, good luck! Let me know how you get on :)!

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.

Hey, remember to change it to your directory ($cngDir) etc.. If this works, post what you did about the permissions and I'll see if I have some time to write an actual fully-developed script for it :)!

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

Hey - Did you run the script? I want feedback ;)

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.

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.

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.

Hey,

So basically, the permissions is a problem with this script and I had problems with the fact that Apache cannot access crontasks (For some reason). I was able to (In terminal) run "crontask /crontask.txt" which would execute the crontask, however, from PHP it would not run it. It's weird, I'll have a look later when I get some time :)!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.