Solved

How to write a Cron?

Posted on 2015-02-16
7
124 Views
Last Modified: 2015-02-20
It looks pretty simple, but I'm having a hard time figuring out how you actually do it...

First of all, can I use Notepad++
Secondly, after you finish writing your script, do you save it and upload it to var/spool/cron
What's the file extension of a cron
Can I practice this on my local environment before I go live
If I open up a cron directory on my server, should I expect to see some scripts in that directory

Basic, basic questions, but I'm confused when I see "command line" verbiage and, what appears to be a straight ahead text editor.

Any direction would be appreciated.

I believe I'm on a Linux server based on the fact that I see var/spool/cron and I'm assuming that's consistent with Linux architecture. But I've never used command line, so feel free to break it down to offensively simply terms if that's what it takes.

Thanks!
0
Comment
Question by:brucegust
7 Comments
 
LVL 11

Assisted Solution

by:Radek Baranowski
Radek Baranowski earned 125 total points
ID: 40612708
you dont place cron files, instead on the linux system you use
crontab -e
command to edit cron jobs. I suppose you already know the syntax.
then you save it as in vim with Esc, :w
after that you need to check if your job launches as you wanted, on specified time/day etc

this should work on any Linux so you might as well run some linux distro in a virtual box/vmware before you start messing with any production system

sample job /w syntax:
# Minute   Hour   Day of Month       Month          Day of Week        Command    
# (0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)                
    0        2          12             *                *            /usr/bin/find
0
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 125 total points
ID: 40612727
Here is the format info: http://en.wikipedia.org/wiki/Cron  On shared hosting you may have to run a program to create your crontab because access is restricted.
0
 
LVL 12

Accepted Solution

by:
Kent W earned 125 total points
ID: 40612757
I'll share my method, it's easy and keeps everything "central".

Most of my crons run as root, so under /root/ I simply create (with "nano", not vim), a file called "cronfile".
In that file, I list all of my cron jobs with the proper syntax.  If you study just a little with the above resources, it's quite understandable.

Once you have all the jobs you want to run in that file, you load it into cron with the simple
crontab /root/cronfile
That's it.  All are sucked into to cron.  When you make changes, you simply edit that cronfile text file, then flush and reload, as simple as -
crontab -r
crontab /root/cronfile

Then use
crontab -l
to list your current cron jobs and make sure your edit took.
I've been using the method for years, and many other admins I've worked with now use the method.  It may work for you, too.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:brucegust
ID: 40612781
Guys, I'm stoked because your counsel combined with everything else that I've googled convinces me that this is pretty straight forward.

The syntax that I'll need is probably the least of my worries, as it's just going to be something that runs early in the morning, once a day.

But here's where I get lost, simply because it's a new environment. When I look at articles such as https://help.ubuntu.com/community/CronHowto and http://artoflinux.blogspot.com/2009/08/cron-jobs-tutorial-for-beginners.html, the code is easy, but I'll see references to "nano" and "vim." I'm getting the impression that, while the code is intuitive, I'm using a different kind of interface to create the command in that it's not a text editor per se, correct?

I'm looking at links like this: http://www.thefreecountry.com/webmaster/sshandtelnet.shtml

So, imagine you're looking over my shoulder and once you've gotten over the abundance of ignorance that charcterizes this poor fool trying to figure all of of this out, how would you guide me as far as:

the interface I need to use
explaining how it differs from a program like Notepad++ and why I won't need to upload anything via an
ftp protocol (how am I popping the hood on my server?)
is is possible to test this on my local enviornment before I go live on my server
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 40612800
"nano" and "vim" are text editors.  Any Unix/Linux system can run 'cron'.  But like @Radek said, you use 'crontab -e ' to create your entry.  FTP isn't of any use.  I think there is more to it than just creating a file.  And I never thought the code was intuitive.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 total points
ID: 40612868
You can write your CRON jobs in plain old PHP, and store them on your own site.  Your hosting company may have a cPanel or similar that gives you an administrative interface to the server.  You may be able to find a place to set up the CRON job trigger(s), or you may want to ask your host for assistance.  You just need to know when the job is supposed to run and what URL path should be triggered.

A couple of things to consider... Your CRON job will not run under the same environment as a regular web script, so your directory paths will not be the same.  Your CRON job will not have any browser output (some installations consider browser output from a CRON to be a malfunction).  This means you will want to learn about how to log the errors, or how to email debugging information to yourself.  Your CRON job will have no browser input, so there will be no session or cookies presented when it's started.

This may be helpful.
<?php // demo/cron_job.php

/**
 * There are significant environmental differences between web scripts and CRON jobs.
 * File paths and parts of the PHP configuration may not be the same.  Database
 * connections may need to be changed.  There is no browser input or output.
 *
 * You can use this script to discern the differences.  Run it as a regular web page
 * and also as a CRON job.  You can use the emails you receive to see the differences.
 *
 * Just put your own email address into the last line, and let it run.
 */
error_reporting(E_ALL);

ob_start();
phpinfo();
$info = ob_get_clean();
mail('You@Your.org', 'CRON INFO', $info);

Open in new window

0
 

Author Closing Comment

by:brucegust
ID: 40622173
Thanks, guys!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP and Soap 3 30
Phone Dialer 5 38
Return data with AJAX, JQUERY and PHP 13 38
Programatically extract date from website 8 20
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now