Question

"cronjob-like" system

Asked by: HMoellendorf

I need to perform continuous actions, which should have been timed, like cronjobs. Therefore, every job is a function of a php class. So I want to write an own "cronjob-like" system.

Does anyone know, how to create such a system?

Kind regards
Henning

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2007-09-18 at 11:51:25ID22836886
Topics

MySQL Server

,

PHP and Databases

Participating Experts
2
Points
500
Comments
26

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Cronjob to execute a webpage
    Hi, Whats the command to create a cronjob that visits a website at a set time, i.e. 2200 at night, and the webpage is a .php one, basically the php page contains information that sends out sms messages at that time, but I need a cronjob so the page is loaded at that time. T...
  2. CronJob misery
    Ok, I have a php script that needs to always be active (runs in a while(true)), I know in windows I just need to make it a service and it'll be fine, always run like it should. But how do I do that in linux? My first thought was a CronJob but that will just exec the script...
  3. cronjob
    how do i write cronjob to run perl script /u/rshah/extract.pl daily morning at 8 AM also i m using tcsh shell the cronjob sends an email notification everytime it executes. i don't want that.
  4. cronjob
    i have a cronjob that runs rahul.pl rahul.pl contains $string .= "\nTesting\n"; #$string =~ s/\n/\\\n/g; #$string =~ s/'/'"'"'/g; system "ssh", "out1", "/u/rshah1/temp.pl", "'$string'"; it does not works it does...
  5. cronjob > php script > execution time
    Does a php page wich is called upon by a cronjob have to be executed in the time specified by in the php.ini file?

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: philjones85Posted on 2007-09-18 at 11:55:14ID: 19915607

unless you can setup a php program on the machine to run as a daemon, you're probably going to need to use the system cron.  just create your own php programs and call them from cron.

 

by: HMoellendorfPosted on 2007-09-18 at 11:56:43ID: 19915612

Hi philones85,

I thought, that I just run one php-file per lynx -dump on a machine and thus file does the planned processes and saves the concurrent log.

Do you have additional tipps?

Kind regards
Henning

 

by: glcumminsPosted on 2007-09-18 at 11:58:45ID: 19915625

You can create a PHP control script to run as a daemon. In daemon mode, it will always be running, so you can execute your timed actions at will.

Here are a few links to get you started:

Multithreaded Daemon in PHP: http://phpmultithreaddaemon.blogspot.com/
Unix Daemon Server Programming: http://www.enderunix.org/documents/eng/daemon.php
PHP Process Control Functions: http://us3.php.net/manual/en/ref.pcntl.php

 

by: philjones85Posted on 2007-09-18 at 12:00:59ID: 19915635

You can do it that way, but that requires that you run lynx every time you want the file to execute.  If you use the local cron, it will do it automatically.  If you're managing many machines, and change the frequency that you want to execute the program often, using one system's cron to run lynx and execute the code on all the other machines is an option...

 

by: HMoellendorfPosted on 2007-09-18 at 12:05:29ID: 19915658

My target is, that one script processes all required functions when it is needed. In example:

every hour: checking for new bills
every day: booking of money transfers
every day: updating old/outdated contracts
every 2 hours: Checking integrity

Something like that. And, because of its complexity and volume of jobs, I'd prefer an system with one php-file... In the best case with mysql database as backend.

Kind regards
Henning

 

by: glcumminsPosted on 2007-09-18 at 12:14:24ID: 19915748

Then a PHP daemon is really your only choice. The script must run all the time, because there is no 'boot-strap' option that will cause it to wake up at the desired times (outside of an external helper like cron).

Your daemon will simply check if any existing jobs need to be done, and execute them. Then it will sleep for the amount of time you specific (using the sleep() function), and wake up again to check for jobs.

I use a program like this to check for incoming data feeds for a product comparison site, and it works quite well.

 

by: philjones85Posted on 2007-09-18 at 12:14:52ID: 19915751

you can do that all through one php script with a cron (if its all on one system)

just have the cron job call the script with command line arguments, for instance:

/etc/crontab - add the lines
01 * * * * user_id /path/to/php/file.php new_bills
01,03,05,07,09.... * * * * user_id /path/to/php/file.php check_integrity
01 04 * * * user_id /path/to/php/file.php money_transfers
01 05 * * * user_id /path/to/php/file.php update_contacts

/path/to/php/file.php
<?php

if ($argv[1] == 'new_bills')
{
  // check for new bills
}
if ($argv[1] == 'money_transfers')
{
  // booking of money transfers
}
if ($argv[1] == 'update_contracts')
{
  // update old/outdated contracts
}
if ($argv[1] == 'check_integrity')
{
  // check integrity
}
?>

 

by: philjones85Posted on 2007-09-18 at 12:18:08ID: 19915772

furthermore, you could set your php script up to allow you to run any function you wanted manually through your wget call as well if you have a need to run something immediately

<?php

if ($argv[1] == 'new_bills' || $_GET['action'] = 'new_bills')
{
  // check for new bills
}
if ($argv[1] == 'money_transfers' || $_GET['action'] == 'money_transfers')
{
  // booking of money transfers
}
if ($argv[1] == 'update_contracts' || $_GET['action'] == 'update_contracts')
{
  // update old/outdated contracts
}
if ($argv[1] == 'check_integrity' || $_GET['action'] == 'check_integrity')
{
  // check integrity
}
?>

then you could call it with
wget www.domain.com/path/to/file.php?action=check_integrity

 

by: HMoellendorfPosted on 2007-09-18 at 12:18:27ID: 19915774

Whats about a table, which looks like a cron table, with hours, minutes and so on? Does it also work?

Kind regards
Henning

 

by: glcumminsPosted on 2007-09-18 at 12:20:44ID: 19915791

Yes, that will work. Your script checks the table, selected jobs with times matching or near the current time, and execute them.

 

by: HMoellendorfPosted on 2007-09-18 at 12:21:26ID: 19915794

Do you have a tut for something like that?

Kind regards
Henning

 

by: philjones85Posted on 2007-09-18 at 12:21:56ID: 19915799

do you have a reason why you don't want to use cron?  are you planning on creating a daemon?

 

by: glcumminsPosted on 2007-09-18 at 12:23:59ID: 19915814

No, I do not know of any tutorial that would cover the specific type of application you are requesting. However, you can break it down into these steps:

1. Create the database that holds the job information, including execution time.
2. Create the script to access the database, and compare the current time to the scheduled job time.
3. Create the job execution portion of the script.
4. Add the process control/daemon portion to the script so that it will remain running.

 

by: HMoellendorfPosted on 2007-09-18 at 12:28:18ID: 19915849

The reason is, that it is a CRM and billing system, coded by myself. My target is, that the system is dynamically and can be edited. Also the script execution for updating the services. So I thought, to create a php-script which will be executed every minute and checks for the tasks to do.

Kind regards
Henning

 

by: glcumminsPosted on 2007-09-18 at 12:33:55ID: 19915899

You can certainly use cron for the "executed every minute" part, if you desire. Then the script itself will only have to check the available jobs, and execute them.

 

by: HMoellendorfPosted on 2007-09-18 at 12:38:39ID: 19915940

Hi glcummins,

thats my target ;o)
But I dunno how to create such a system for the jobs, that has to be executed? Only one idea:
Last run, grace period, name, description, command.

Is that a start?

Kind regards
Henning

 

by: glcumminsPosted on 2007-09-18 at 12:42:34ID: 19915969

Yes, that sounds right. If grace period is larger than (current time - last run), then run the job, and update last run.

You will also want to build a good logging system, so that you can see what happened if something breaks. Since this will not be a screen-output program, you will need it to log extensively to a file or database.

Finally, you will want to make sure that the program does not step on it's own toes. It needs to be able to check if another instance of the program is already running (from the previous minute's execution), and exit gracefully as needed.

 

by: HMoellendorfPosted on 2007-09-18 at 12:52:00ID: 19916037

I thought for the following table structure:

jobid | jobname | jobran | jobperiod (in seconds) | jobrunning

Is that ok?

 

by: glcumminsPosted on 2007-09-18 at 12:55:44ID: 19916062

The only thing I would comment on is 'jobrunning'. The current script will know if there is a job running, so the only benefit that field will have is to another instance of the script. I would recommend that you put a system in place that prevents multiple instances of the script from running, so that you avoid problems with hung jobs. If your script is running and hanging for some reason, you don't want 57 instances of the script running in memory before you find it about an hour later.

You can create a lock file with your script that will notify future instances that the script is still running. If the lock file exists, the new instance simply exists, and tries again on minute later.

 

by: HMoellendorfPosted on 2007-09-18 at 12:59:46ID: 19916093

But, there could be many instances running because of many jobs. One job should run only a one instance.

Kind regards
Henning

 

by: glcumminsPosted on 2007-09-18 at 13:08:40ID: 19916164

Hm... Okay. In that case, your table structure suggestion was correct. You should have something in place, however, to recognize hung processes and deal with them as necessary.

 

by: HMoellendorfPosted on 2007-09-18 at 13:10:45ID: 19916181

And how? :S

 

by: glcumminsPosted on 2007-09-18 at 13:20:10ID: 19916259

Well, the 'how' is up to you. There are a number of possibilities:

1. Specify a 'reasonable' job time. Add a 'startTime' field to the table. Each time your script runs, look for jobs that have been running for longer than that time period and kill as necessary.

2. Have your script check the output of 'top' for processes matching the name of your script. Check the TIME field, and kill any that have been running longer than they should.

3. Set a maximum number of concurrent instances that can run. Increment a field in the database each time a script starts, and decrement it when the script ends. If a script checks this field and finds that too many instances are running, it can exit, exit and notify (perhaps by email), or kill the oldest script.


 

by: glcumminsPosted on 2007-09-18 at 13:21:07ID: 19916267

You will need to explore the possibilities to determine which one fits your coding style, your experience, and your overall program structure.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...