Category Archives: Scripting

A Short Introduction To Cron Jobs

Version 1.0
Author: Falko Timme <ft [at] falkotimme [dot] com>
Last edited 03/08/2009

This article is a short introduction to cron jobs, their syntax, and how to set them up. A cron job is a scheduled task that is executed by the system at a specified time/date.

I do not issue any guarantee that this will work for you!

1 crontab

The command to create/edit, list, and remove cron jobs is crontab. If you call it with the -u option, it specifies the name of the user whose crontab is to be tweaked. If this option is not given, crontab examines “your” crontab, i.e., the crontab of the person executing the command. If you are looged in as root and run crontab without -u, then root’s crontab is listed/modified/removed. If you are logged in as exampleuser and run crontab without -u, then exampleuser‘s crontab is listed/modified/removed.

Examples:

crontab -l

lists the cron jobs of the user as that you are currently logged in:

server1:~# crontab -l
* * * * * /usr/local/ispconfig/server/server.sh > /dev/null 2>> /var/log/ispconfig/cron.log
30 00 * * * /usr/local/ispconfig/server/cron_daily.sh > /dev/null 2>> /var/log/ispconfig/cron.log
server1:~#

crontab -u exampleuser -l

lists all cron jobs of exampleuser.

crontab -e

let’s you create/modify the cron jobs of the user as that you are currently logged in (I’ll come to the syntax in the next chapter).

crontab -u exampleuser -e

let’s you create/modify the cron jobs of exampleuser.

crontab -r

deletes all cron jobs of the user as that you’re currently logged in.

crontab -u exampleuser -r

deletes all cron jobs of exampleuser.

If you have written your cron jobs to a text file, you can use the text file to create the cron jobs. For example, let’s assume you have created the text file /tmp/my_cron_jobs.txt

vi /tmp/my_cron_jobs.txt

… with the following contents:

30 00 * * * /path/to/script

You can create a cron job from that file as follows:

crontab /tmp/my_cron_jobs.txt

(Or for exampleuser:

crontab -u exampleuser /tmp/my_cron_jobs.txt

)

Please note that this will overwrite all previously created cron jobs – if you’ve already created some cron jobs, you better use crontab -e and add the new cron job manually.

See

man crontab

to learn more about the crontab command.

2 Cron Job Syntax

A cron job consists out of six fields:

<minute> <hour> <day of month> <month> <day of week> <command>

field          allowed values
—–          ————–
minute         0-59
hour           0-23
day of month   1-31
month          1-12 (or names, see below)
day of week    0-7 (0 or 7 is Sun, or use names)

// <![CDATA[// <![CDATA[
document.write('

‘);
// ]]>

// <![CDATA[// <![CDATA[
if (typeof ord=='undefined') {ord=Math.random()*10000000000000000;}
document.write('’);
// ]]>// <![CDATA[// <![CDATA[
document.write('’);
// ]]>// <![CDATA[// &lt;a target=”_blank” href=”http://ad.doubleclick.net/click%3Bh=v8/3880/3/0/%2a/x%3B215487674%3B0-0%3B0%3B37706476%3B4307-300/250%3B31741543/31759419/1%3Bu%3Didgt-97517877_1249433519%2C11228fcd2c86b5a%2Cvirtualization%2Cidgt.virtualization_H%3B%7Eaopt%3D0/ff/53/ff%3B%7Efdr%3D216157216%3B0-0%3B0%3B39511760%3B4307-300/250%3B31848342/31866218/1%3Bu%3Didgt-97517877_1249433519%2C11228fcd2c86b5a%2Cvirtualization%2Cidgt.virtualization_H%3B%7Eaopt%3D3/1/53/0%3B%7Esscs%3D%3fhttp://www.vwire.com/vwire.cfm”&gt;&lt;img src=”http://m1.2mdn.net/2251067/1-error_300x250_vR.jpg&#8221; width=”300″ height=”250″ border=”0″ alt=”” galleryimg=”no”&gt;&lt;/a&gt; &lt;a href=”http://a.collective-media.net/jump/idgt.howtoforge.en/article_above;sec=article;fold=above;tile=3;sz=300×250;ord=123456789?&#8221; target=”_blank”&gt;&lt;img src=”http://a.collective-media.net/ad/idgt.howtoforge.en/article_above;sec=article;fold=above;tile=3;sz=300×250;ord=123456789?&#8221; width=”300″ height=”250″ border=”0″ alt=””&gt;&lt;/a&gt; // <![CDATA[// <![CDATA[
document.write('

‘);
// ]]>

When specifying day of week, both day 0 and day 7 will be considered Sunday.

A field may be an asterisk (*), which always stands for first-last.

Names can also be used for the “month” and “day of week” fields. Use the first three letters of the particular day or month (case doesn’t matter), e.g. sun or SUN for Sunday or mar / MAR for March..

Let’s take a look at the two cron jobs from the first chapter:

* * * * * /usr/local/ispconfig/server/server.sh > /dev/null 2>> /var/log/ispconfig/cron.log

This means: execute /usr/local/ispconfig/server/server.sh > /dev/null 2>> /var/log/ispconfig/cron.log once per minute.

30 00 * * * /usr/local/ispconfig/server/cron_daily.sh > /dev/null 2>> /var/log/ispconfig/cron.log

This means: execute /usr/local/ispconfig/server/cron_daily.sh > /dev/null 2>> /var/log/ispconfig/cron.log once per day at 00:30h.

The day of a command’s execution can be specified by two fields: day of month, and day of week. If both fields are restricted (i.e., aren’t *), the command will be run when either field matches the current time. For example, 30 4 1,15 * 5 would cause a command to be run at 4:30h on the 1st and 15th of each month, plus every Friday.

You can use ranges to define cron jobs:

Examples:

1,2,5,9 – means every first, second, fifth, and ninth (minute, hour, month, …).

0-4,8-12 – means all (minutes, hours, months,…) from 0 to 4 and from 8 to 12.

*/5 – means every fifth (minute, hour, month, …).

1-9/2 is the same as 1,3,5,7,9.

Ranges or lists of names are not allowed (if you are using names instead of numbers for months and days – e.g., Mon-Wed is not valid).

1,7,25,47 */2 * * * command

means: run command every second hour in the first, seventh, 25th, and 47th minute.

Instead of the first five fields, one of eight special strings may appear:

string         meaning
——         ——-
@reboot        Run once, at startup.
@yearly        Run once a year, “0 0 1 1 *”.
@annually      (same as @yearly)
@monthly       Run once a month, “0 0 1 * *”.
@weekly        Run once a week, “0 0 * * 0”.
@daily         Run once a day, “0 0 * * *”.
@midnight      (same as @daily)
@hourly        Run once an hour, “0 * * * *”.

You can also use name=value pairs in a crontab to define variables for the cron jobs:

# use /bin/bash to run commands, instead of the default /bin/sh
SHELL=/bin/bash
# mail any output to exampleuser, no matter whose crontab this is
MAILTO=exampleuser
# set the PATH variable to make sure all commands in the crontab are found
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

* * * * * my_command

Please note: unless you set a PATH variable in a crontab, always use full paths in the crontab to make sure commands are found and can be executed. For example, instead of writing rsync, you should write /usr/bin/rsync. Use which to find out the full path of a program:

which rsync

server1:~# which rsync
/usr/bin/rsync
server1:~#

See

man 5 crontab

to learn more about the cron job syntax.

Read more on Howtoforge.com

Advertisement

Dangerous coding errors revealed

The US National Security Agency has helped put together a list of the world’s most dangerous coding mistakes.

The 25 entry list contains errors that can lead to security holes or vulnerable areas that can be targeted by cyber criminals.

Experts say many of these errors are not well understood by programmers.

According to the SANS Institute in Maryland, just two of the errors led to more than 1.5m web site security breaches during 2008.

Read more on news.bbc.co.uk

How to backup MySQL databases, web server files to a FTP server automatically

This is a simple backup solution for people who run their own web server and MySQL server on a dedicated box or VPS. Most dedicated hosting provider provides the backup service using NAS or FTP servers. These service providers will hook you to their redundant centralized storage array over private VLAN. Since I manage couple of boxes, here is my own automated solution. If you just want a shell script, go here (you just need to provided appropriate input and it will generate FTP backup script for you on fly).

Read more at nixCraft

PHP Access To An MSSQL Database From Debian Etch With ODBC And FreeTDS

PHP Access To An MSSQL Database From Debian Etch With ODBC And FreeTDS

This assumes you already have Apache2 and PHP5 set up properly on your system. My efforts to get this connection working were compiled from information found at http://www.unixodbc.org and http://www.freetds.org. These steps worked for me with an Apache2 web server with php5 running on Debian Etch stable in October of 2007. The SQL server is running Microsoft SQL 2005 on a Windows 2003 Server OS.

In these instructions I assume you’ve su’d to a root account. I also use joe as my editor so replace that with your editor of choice.

Read more at Howtoforge.com

Integrating eAccelerator Into PHP5 (CentOS 5.0)

Version 1.0
Author: Falko Timme <ft [at] falkotimme [dot] com>
Last edited 09/14/2007

This guide explains how to integrate eAccelerator into PHP5 on a CentOS 5.0 system. From the eAccelerator project page: “eAccelerator is a free open-source PHP accelerator, optimizer, and dynamic content cache. It increases the performance of PHP scripts by caching them in their compiled state, so that the overhead of compiling is almost completely eliminated. It also optimizes scripts to speed up their execution. eAccelerator typically reduces server load and increases the speed of your PHP code by 1-10 times.”

Read more at Howtoforge.com

How To Install Sun Java SE 6 JDK and NetBeans 5.5.1 – Fedora 7

Version 1.0
Author: Francis Theys  <francis [at] fusiontek [dot] be>, Falko Timme <ft [at] falkotimme [dot] com>, Till Brehm <t [dot] brehm [at] projektfarm [dot] de>
Last edited 08/10/2007

This tutorial provides step-by-step instructions about how to create a highly available VMware Server environment on a Debian Etch system. With this tutorial, you will be able to create Virtual Machines that will be available on multiple systems with failover/failback capabilities.
Remove ads

The system is based using components of “The High Availability Linux Project” , namely “DRBD” and “Heartbeat”.
The free open-source edition of DRBD will only allow a 2-node active/passive environment, so this is not for large businesses!. Also, the heartbeat/drbd setup configured in this tutorial, is by using 2 Ethernet NIC’s. I recommend that at least the nic to be used for DRBD replication (eth1 in this tutorial) is 1Gbit or more.

Warning: This tutorial requires at least basic Linux & Networking knowledge and is not for so called “newbies” to Linux and/or Networking and/or Vmware !

Read more at Howtoforge.com

How To Harden PHP5 With Suhosin On Fedora 7

Version 1.0
Author: Falko Timme <ft [at] falkotimme [dot] com>
Last edited 07/16/2007

This tutorial shows how to harden PHP5 with Suhosin on a Fedora 7 server. From the Suhosin project page: “Suhosin is an advanced protection system for PHP installations that was designed to protect servers and users from known and unknown flaws in PHP applications and the PHP core. Suhosin comes in two independent parts, that can be used separately or in combination. The first part is a small patch against the PHP core, that implements a few low-level protections against bufferoverflows or format string vulnerabilities and the second part is a powerful PHP extension that implements all the other protections.”

Read more at Howtoforge.com

Installing The PHP-MemCache Module On CentOS 5.0

Author & Content by Tim Haselaars (http://www.trinix.be)

“MemCached” is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

In this howto I explain how you can very easily install it and make it avaible in PHP.

You need to activate the RPMForge custom repository (formely known as Dag Wieers): http://dag.wieers.com/rpm/packages/rpmforge-release/

Grab your specific RPM and install it:

wget http://dag.wieers.com/rpm/packages/rpmforge-release/rpmforge-release-*.rpm
rpm –install rpmforge-release-*.rpm

yum install –enablerepo=rpmforge memcached

Now memcached is installed.

You can test it:

memcached -m 512 -u nobody -vv

First, you start up the memcached daemon on as many spare machines as you have. The daemon has no configuration file, just a few command line options, only 3 or 4 of which you’ll likely use:

./memcached -d -m 2048 -l 10.0.0.40 -p 11211 -u nobody

This starts memcached up as a daemon, using 2GB of memory, and listening on IP 10.0.0.40, port 11211. The -m switch specifies the amount of memory in megabytes. The -l switch specifies the IP to listen on and finally the -p switch specifies the port to listen on. The default port is 11211 and if your machine has just 1 IP you can omit the -l parameter. In the above example I set the amount of memory to 2GB. Of course you should use a sensible amount of memory. Making your machine swap to disk sort of defeats the purpose of a memory cache daemon. Note that it’s perfectly fine to run the memcached daemon on another machine than the one you’re running your actual PHP project on. You could even set up a machine totally dedicated to being a memory cache server. And if that’s not even enough you can set up multiple servers as well. The sky is the limit. Note: if you try to start the memcached as root it will require you to specify a user under which it should run with an additional -u nobody.

Installation of the PHP MemCache extension:

cd /files/download/

wget http://pecl.php.net/get/memcache-2.1.2.tgz

tar -xvf memcache-2.1.2.tgz

cd memcache-2.1.2

phpize && ./configure –enable-memcache && make

Copy the file memcache.so to the default module directory.

vi /etc/php.ini

Add

extension=memcache.so

/etc/init.d/httpd restart

If you check your phpinfo() you should see a MemCache section appear.

You can now fully use the MemCache functionality in your PHP.

Read more at Howtoforge.com

Five scripts that make life easier with Vim

By: Joe ‘Zonker’ Brockmeier

The Vim editor allows you to modify its behavior via scripts, and the Vim community has produced hundreds of scripts that may help you be more productive, or add functions to Vim that you’ve always wished it would have. Here are five that I find particularly useful.

To install most scripts, copy them to your ~/.vim/plugins directory and restart Vim. Each script should (and usually does) have instructions on installing it, so check the script’s page for additional directions.

Read more at Linux.com

Introducing Remo – An Easy Way to Secure an Insecure Online Application

Say you have a nasty application on your Apache webserver that has been installed by some jerks from the marketing department and you can neither patch nor remove it. Maybe it is a problem of ressources, a lack of know-how, a lack of source-code, or possibly even due to political reasons. Consequently, you need to protect it without touching it. There is ModSecurity, but they say this is only for experts. A straightforward alternative is Remo, a graphical rule editor for ModSecurity that comes with a whitelist approach. It has all you need to lock down the application.

Read more at Howtoforge.com