Running cygwin cron from windows as a service

2 Running cygwin cron from windows as a service
Cron is a program for running scripts or programs at certain times. For cron to be able to do this,
it has to be running when the machine is on. This is done by adding cron as a service. Open a
bash terminal. Write:

cygrunsrv -I cron -p /usr/sbin/cron -a
(do cygrunsrv –help to better understand).

Now we need to setup cron so it runs what we want. If you don’t like vi you should do export

Type crontab to better understand cron’s options

In a text editor, write the following to a file called cronrun.txt

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

0 3 * * * /bin/backup > /dev/null 2>&1

Then type
crontab cronrun.txt to load your job. Type crontab -l to list your jobs

The first line tells cron to run the commands in bash, second is the path cron searches for
programs, third who to mail on error and fourth which directory to default to The last line is a
cron line which tells cron to run backup at 0300 each day and don’t report anything even if there
is an error. Lets have a look at how its done.

* * * * * command line
| | | | |
| | | | |- weekday (0-6)
| | | |- month (1-12)
| | |- dayofmonth (1-31)
| |- hours (1-23)
|- minutes (0-59)

The first five entries correspond to times, an * means any time. For example the entry ”0 1 * *
0” correspond to 0100 am on a Sunday. You can also separate times with comma, for example ”
0 1 * * 1,3,5 /bin/backup” runs the backup script 0100 on Monday ,Wednesday and Friday. The
> /dev/null redirects standard output from backup to a black hole, the 2>&1 redirects standard
error to standard output from backup, thus redirecting standard error to a black hole.

X windows with Cygwin

PC XStation Configuration
Download the CygWin setup.exe from http://www.cygwin.com.

Install, making sure to select all the XFree86 optional packages.

If you need root access add the following entry into the /etc/securettys file on each server:

<client-name>:0
From the command promot on the PC do the following:

set PATH=PATH;c:\cygwin\bin;c:\cygwin\usr\X11R6\bin
XWin.exe :0 -query <server-name>
The X environment should start in a new window.

Many Linux distributions do not start XDMCP by default. To allow XDMCP
access from Cygwin edit the “/etc/X11/gdm/gdm.conf” file. Under the
“[xdmcp]” section set “Enable=true”.

If you are starting any X applications during the session you will need
to set the DISPLAY environment variable. Remember, you are acting as an
XStation, not the server itself, so this variable must be set as
follows:

DISPLAY=<client-name>:0.0; export DISPLAY

Meet Bubba

We had originally planned to slow down after the wedding -
Didn’t happen!
Now that the wedding planning is not consuming our time, this
past Tuesday we decided to apply as foster parents at the Northern VA
Sheltie Rescue Association. We figured they would check our
references, enroll us in a class or two, have us stop my the shelter, and file
papers before allowing us to care for a pup. However, on tuesday
night Bubba was rescued from a local pound. He was scheduled to be put to
sleep yesterday. On Tuesday night, just four hours after the
application was submitted, we were contacted to take Bubba in. He
came to live with us today.

The left pic is how he looked when he was picked up from the nasty dog pound

The right image is how he looks now all cleaned up and minus a few clumps of hair

Bubba is 9 years old and extremely sweet. He is recovering from
a few pound caught illnesses, but is doing well. Currently, the plan
is for him to live with us until an adoptive family steps forward. (This
normally takes 2 plus months.) We look forward to the time we will
have with him.

Reformatting a text field in Microsoft Access to be a true date

I had a text field (DATEOLD) with values like 20030526, that were dates in YYYYMMDD.

I needed these values to exist in a true datetime field in Microsoft
Access. So I created a new field called NEWDATE of the date/time type.

Then I ran the query below to convert the numbers.

UPDATE Itec_data1
SET [NEWDATE] = format(DateSerial (left([DATEOLD],4), mid([DATEOLD],5,2), right([DATEOLD],2)),”yyyy/mm/dd”);

Because dates and times are stored as integers and decimals
respectively, with two datetime fields (DATE and TIME) you can create a
third and final datetime field (DATETIMER) simply by adding them.

UPDATE Itec_data1
SET [DATETIMER] = [DATE]+[TIME];

Sending files over SFTP in an automated fashion

The exerpts below describe how to use SFTP to transfer files from one
machine to another in automated fashion, such as using shell scripts. I
needed to do this as part of a back up script.

Use the ssh authorized_keys functionality instead of trying to pass in
a username and password. Create a key on the client side using
ssh-keygen (it will have to be nonpassworded if you want this to be
fully automated) and copy the public key to the target user’s
authorized_keys file on the ssh/sftp server. For extra security, an
option (“from”) is available to limit the use of that key to
connections coming from your client server. This will give ssh or sftp
access to anyone who comes from the client server with the right
private key (no worse than having a name and password hardcoded in a
script). See the SSH docs for more detail.

Set that up and you’ll be past the authentication issues. Then check
out the -b option of sftp to give it a batch file with sftp commands to
automatically execute:

sftp -b batchfile targetuser@targethost

Now, you have to configure the server ahead of time to consider the source trusted. Use the following steps to make that happen:

create the keys for the origin account, i.e. the account that performs the copy:

$ cd ~/.ssh

$ ssh-keygen -t dsa

You are asked for a passphrase, do not enter a passphrase, type for empty passphrase.

verify the creation of the 2 files:

~/.ssh/id_dsa

~/.ssh/id_dsa.pub

copy ~/.ssh/id_dsa.pub to the destination node

login into the destination node and verify if file ~/.ssh/authorized_keys is already present, if not do:

$ cd ~/.ssh

$ mv id_dsa.pub authorized_keys

Verify ~/.ssh/authorized_keys and add/replace id_dsa.pub as needed.

Then, run your sftp with a -b extension. This will put it in batch mode
and allow it to draw its commands from a text, or batchfile. You need
to specify the batch filename after -b.

—————————————————————————————-

Example 2

Log in to your account on the front-end node (the client machine). Here
we describe the machine you want to login TO as the client (compute
node). In the example below we are using root, but you should likely
use a standard user name. Go to the .ssh directory of your home
directory. If it isn’t there, you may create one with the command

mkdir ~/.ssh

In the examples below, you should conceptually replace /root/ with /home/youruser/.ssh.

Create an rsa key pair by executing ssh-keygen with the

“rsa” option:

ssh-keygen -t rsa

The ssh-keygen program will respond with:

Generating a public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

Type the Enter key to accept the default value:

[Enter]

The ssh-keygen program will respond with:

Created directory ‘/root/.ssh’.

Enter passphrase (empty for no passphrase):

Type the Enter key to accept the default value:

[Enter]

The ssh-keygen program will respond with:

Enter same passphrase again:

Type the Enter key again:

[Enter]

The ssh-keygen program will respond with:

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

[fingerprint] root@[hostname]

Create the ssh directory for each compute node’s root

account (on the first use of ssh the system automatically

creates the ssh directory for you).

Login to a compute node. Type:

ssh root@[compute node address]

Enter the root password.

Check if a .ssh directory exists. Type:

ls -la

If there is no .ssh directory listed, type:

mkdir .ssh

ls -la

chmod go-rwx .ssh

ls -la

The .ssh listing should look like:

drwx—— 2 root root 4096 [date & time] .ssh

The .ssh directory is now only accessible by the user root.

Log out of the compute node. Type:

logout

Use sftp (secure ftp) to copy the generated rsa public key

to each compute node’s root account secure shell directory

as the file authorized_keys. Type:

sftp root@[compute node address]

If prompted to continue connecting, type:

yes

Log in with the root password for that compute node.

You will get the sftp prompt:

sftp>

Change to the secure shell directory. Type:

cd .ssh

Copy the rsa public key to the compute node. Type:

put /root/.ssh/id_rsa.pub authorized_keys

Exit sftp. Type:

exit

Repeat this procedure for each compute node.

To test that the secure automatic login is working

properly from the front-end node, type:

ssh root@[compute node address]

The system should log you in without prompting for a

password.

Log out of the compute node. Type:

logout

Also copy the rsa public key to the authorized_keys file on

the front-end node. Type:

cp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys

root on the front-end node can now securely access all nodes

in the cluster without having to enter a password.

Selecting Date from a Mysql field in specific format

I had a membership table with users and the datetime at which they
joined the site. I needed to get a count of members joining per month,
so I did this:

1
2
3
4
5
SELECT DATE_FORMAT( regdate, '%b %Y' ) <br />
AS
MONTH , count( uid ) AS Users <br />
FROM gl_users <br />
GROUP BY MONTH  LIMIT 0 , 30<br />

Which resulted in

SQL result

SQL-query: SELECT DATE_FORMAT( regdate, ‘%b %Y’ ) AS
MONTH , count( uid ) AS Users
FROM gl_users
GROUP BY MONTH LIMIT 0, 30;

MONTH Users
Apr 2004 41
Dec 2003 39
Feb 2004 41
Jan 2003 3
Jan 2004 64
Jul 2004 7
Jun 2004 53
Mar 2004 45
May 2004 47
Nov 2003 51
Oct 2003 1

And here are some more resources that I found.

http://dev.mysql.com/doc/mysql/en/Date_and_time_functions.html

http://www.sitepoint.com/forums/archive/index.php/t-164068


http://www.databasejournal.com/features/mysql/article.php/10897_2190421_2

Automate your life

What is crontab?

If you’ve worked with Perl
scripts I’m sure you’ve heard or seen the words “cron”,

“crontab”, or “cron job” before. If not, you’ll learn
something new today!

The

crontab command allows you to tell your server to execute a file at a specific

time, as often as you want – like once a day or once a minute. Most commonly,
crontab

is used to execute a cgi script to perform a certain task repeatedly rather than

doing it manually. Saves a lot of time!

NOTE: many hosting
companies disable crontab on their hosting accounts because

it is often abused. Using it to repeatedly execute a file takes up precious cpu

time and if misused can slow down a server immensely. However, if you talk to
your

hosting company and let them know what file you are going to run they more than

likely will be happy to enable crontab on your system. If they won’t, then you
might want to think about getting a dedicated server…

Where do I start?

To use crontab, you have
to be able to telnet into your server. This is accomplished

in Windows by going to Start->Run and typing in “telnet yourdomain.com”
and hitting

“OK”. A new window will pop up and you will have to put in your
username and password.

If you normally use an FTP client, usually it’s the same username and password.

If successful. you will
then get a command prompt: $

First, you can see the
crontab usage info by typing in “crontab” and hitting return.

Here’s what it looks like on my server:

usage: crontab [-u user]
file

crontab [-u user] { -e | -l | -r }

-e (edit user’s crontab)

-l (list user’s crontab)

-r (delete user’s crontab)

So, if you type:

crontab -l

you will get a list of the crontab jobs already running on your system. Try it
out. You probably don’t have any running so you will get an empty list…

How do I set up a
crontab job?

While you can edit the crontab file directly through telnet, I’ve found that the
easiest way for a beginner to start a crontab job is to create a text file
containing your crontab instructions, upload it to your main directory, telnet
into your system, and then just type:

crontab myfile.txt

and the crontab job will
be created.

So what do I put in the text file? What is the syntax?

This text file will
minimally only have to have one line containing the information

for your cron job. Here is a run down of the syntax:

0 1 * * * /path/to/cgi-bin/yourscript.cgi

| | | | |

| | | | |________________ day of week

| | | |__________________ month of year

| | |____________________ day of month

| |______________________ hour of day

|________________________ minute of hour

The * in the above example
basically means “every”. So, in this example the script would execute
*every* month, *every* day of the week, *every* day of the month, and then ONLY
at hour 1 of the day. So – this script would execute once a day at 1 am in the
morning.

Say you wanted to execute
it 4 times a day, you would type in:

0 1,7,13,19 * * * /path/to/cgi-bin/autocron.cgi

This executes autocron.cgi
four times a day about every 6 hours. Notice that

you can use commas to add in more times – but always keep the spaces to
delineate

the time frames.

Another example, to
execute autocron.cgi twice a month:

0 1 1,15 * * /path/to/cgi-bin/autocron.cgi

This line would execute
the file on the 1st and 15th of the month at 1AM.

Can I run multiple
crontab jobs?

In a nutshell – YES! Just
add multiple lines to the text file using the syntax above for every

script that you would like to run. Make sure that the paths to the scripts are

correct and you’ve double checked your time settings.

How can I tell that it’s working?

Simple, after you have
executed the text file with your one line crontab, just

type:

crontab
-l

At your command prompt and you will see something like this:

# (cronjob.txt installed on Tue Apr 11 21:19:12 2000)

# (Cron version — $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)

0 1 * * * /path/to/cgi-bin/yourscript.cgi

All of the running jobs
will be listed.

Can I edit my
crontab file through telnet?

Yes, however, I prefer to
just delete the current crontab file and create a new

one with a new text file. Just type:

crontab
-e

and your crontab file editor will pop up and you can edit the file. Some telnet

clients make it hard to edit this file though – which is why I just delete

and recreate my crontab file.

How do I delete a crontab file?

This is easy! At the
telnet prompt, type:

crontab
-r

and the crontab file will be completely deleted. You can check this by using the

“crontab -l” command. And of course, you can recreate your file by
typing

“crontab myfile.txt”. (or whatever you named the text file containing
your

crontab lines)

Greed is not synonymous with America

But it’s getting harder to prove that point to myself when I read things like this.

From the Motley Fool via Yahoo
Vanity and Vulgarity at Enron
Thu Jun 3,10:20 AM ET
By Seth Jayson

“In one tape, after enduring a bit of ribbing about “how much money you
guys stole from those poor grandmothers in California,” a trader says,
“Yeah, Grandma Millie, man. But she’s the one who couldn’t figure out
how to (expletive) vote on the butterfly ballotâEuro¦” The first
speaker responds, “Yeah, now she wants her (expletive) money back for
all the power you’ve… jammed right up her (expletive) for (expletive)
250 dollars a megawatt-hour.”

Pride
has always been the fatal flaw. Just look at literature. From Homer to
Hamlet and even Harry Potter (news – web sites), hubris is the vice
that brings down the mighty.

These days, our most visible dramas tend to play out in legal action,
and some pretty compelling language has come to light in the Snohomish
County, Wash., Public Utilities District’s (PUD) efforts to obtain
compensation from Enron (OTC BB: ENRNQ.PK – News) for alleged market
manipulation.

Recent transcripts of conversations between Enron’s West Coast energy
traders offer vivid proof that the firm’s sophisticated, self-obsessed
thieves didn’t just hold a dim view of its unwitting stockholders.
Public entities, like the state of California, were considered easy
prey. Timothy Belden, one of two traders from the Portland office who
have pled guilty and cut a deal with prosecutors, says on one tape that
the firm’s trading practices “just (expletive) California… to the
tune of a million bucks or two a day.”

As you might expect, these guys harbored no affection for the rest of
us, either. You and I — derisively referred to as “Grandma Millie” by
the traders — are looked at as ignorant rubes, ripe for the rip-off.

In one tape, after enduring a bit of ribbing about “how much money you
guys stole from those poor grandmothers in California,” a trader says,
“Yeah, Grandma Millie, man. But she’s the one who couldn’t figure out
how to (expletive) vote on the butterfly ballotâEuro¦” The first
speaker responds, “Yeah, now she wants her (expletive) money back for
all the power you’ve… jammed right up her (expletive) for (expletive)
250 dollars a megawatt-hour.”

Other tapes document traders’ nefarious plans such as sending power
away from areas that needed it, manufacturing grid congestion that
Enron would later be paid to alleviate. They also capture the hope that
then-candidate George W. Bush might someday appoint Ken Lay as U.S.
energy secretary.

“How great would that be for all the players in the market?” one asked.

Pretty great, I’ll bet. And given the unbelievable arrogance displayed
by this gaggle of crooks — they knew these conversations were being
taped, for crying out loud — we should be mighty glad it didn’t come
to pass. But most of all, we should hope that this kind of criminal
conceit is punished as mightily in our courtrooms as it is in fiction.

Linux.com: Bugs in your shell script?

By: Larry Reckner
Topics: Shell
Subsection: Intermediate
If your writing a shell script and want to watch exactly what is going on (very usefull for debugging purposes), add the line

1
set -vx

in the beginning of the script.

The shell script will then output what it’s doing so you can watch.

This can also be done via command line by doing

1
<br /><br />sh -x filename