Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to write a Cron?

Posted on 2015-02-16
7
128 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 83

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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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 83

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 109

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
Fine Tune your automatic Updates for Ubuntu / Debian
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

839 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