Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cron Jobs - General Questions 5

Status
Not open for further replies.

Quintios

Technical User
Mar 7, 2002
482
US
I have something that I'll need to program into a cron job. I've never even attempted to *schedule* a cron job and only recently found out what they were, so I have a lot of questions that will lead to more questions.

1. Does a cron job run a script, or does it need to be a compiled executable?
2. If it's a script, what language can you use to do this?
or
2. If it's a compiled executable, what language can you use to write the source, and how is it compiled?
3. Does the platform (Linux vs. Unix) make a difference?

Thanks in advance for your help!

Onwards,

Q-
 
1. either
2. any that can be executed by your shell or can be defined in the first line of the script (eg #!/bin/someinterpreter )
3. just in the way that you edit and/or install the actual cron schedule. see `man crontab` on your system.


 
If you had to design a script that would FTP something across the Internet, what language would you use?

Onwards,

Q-
 
Korn shell.

Just get the script to work correctly at the command line, then give it to cron.

 
Complete newbie here. Korn shell is a language? Is there a syntax reference out there somewhere?

I was always under the impression that a "shell" was just the window that opened up under *nix in which you edited a text file that contained scripting commands.

/me is completely confused. :)

Onwards,

Q-
 
No problem. I was a newbie once myself. In fact I'm still a newbie in some of the other forums!

A shell in unix is a program that accepts your commands and does something with them. When you log onto unix and type "cd somedirectory", it's the shell that you're talking to. It's the shell that carries out your command. This is similar to CMD.EXE in the MS Windows world. You can also put a bunch of commands into a file and execute them by just executing the file. This is called a shell script and is analogous to a batch file in the MS Windows world.

There are many different shells, each with their own syntax and usage. The most common are the Korn shell (my favorite), the Bourne shell, and the C shell. There are lots of books on the various shells. One good one on the Korn Shell is The Korn Shell by Anatole Olczak. There's also a good web site at
To FTP your file each night, create a file in your home directory called
Code:
nightly_ftp.ksh
. Make it contain the following...
[tt]
#!/bin/ksh

export FILE=filetosent.txt # Change to the file to send
export DESTINATION=127.0.0.1 # Change to where it needs to go

print "FTPing ${FILE} to ${DESTINATION} at $(date)"

ftp ${DESTINATION} <<-FTMCMDS
user <username> <password>
put ${FILE}
bye
FTPCMDS

print &quot;Done!&quot;
[/tt]
You'll need to change the filename to send, the destination machine, and the username and password, but this is a basic shell script to FTP a file somewhere.

Then, go to the command line and type the following...
Code:
   chmod +x nightly_ftp.ksh
This will make the shell script executable. To try it out at the command line, just type...
Code:
   nightly_ftp.ksh
If it's all correct, it should FTP the file to that machine.

Now to set it up as a cron job, make another file in your home directory called [tt]my_crontab[/tt]. Make it contain the following...
Code:
   0 18 * * * nightly_ftp.ksh > nightly_ftp.log 2>&1
This is telling cron to run your script every night at 6pm, and send all output messages from the script to a file called
Code:
nightly_ftp.log
. Then, to make this your active crontab file, type the following command...
[tt]
crontab my_crontab
[/tt]
This is a quick and dirty example, so there may be mistakes in it, but this should get you started. Check the manual pages for the different commands for more information. At the prompt type...
[tt]
man ksh
man chmod
man crontab
man cron
man ftp
[/tt]

Hope this helps.
 
There's also a UNIX Scripting forum on Tek-Tips at...

forum822
You can pick up some good info there too.

Hope this helps.

 
Hope I'm not overcomplicating things here, but many of the problems users get with cron involve the fact that cron itself doesn't use any environment variables, particularly the PATH one (PATH is the list of pathnames you are assigned at login so that your shell knows where it might find certain scripts etc, type echo $PATH to see it). Perhaps the easiest way of overcoming this limitation is to make sure you use absolute (ie full) pathnames when entering them in cron. Alternatively, specify the appropriate PATH and other variables in the script itself.

My tip of the day is to include the following early in your crontab file, so that you always know what each crontab field is without constantly having to refer to the man page:

#Crontab field order:
#minute (0-59),
#| hour (0-23),
#| | day of the month (1-31),
#| | | month of the year (1-12),
#| | | | day of the week (0-6 with 0=Sunday).
#| | | | | command

Hope this helps and good luck.
 
Man, I just got back. What wonderful replies!

Now I just need help installing Linux...

*goes off to find the Linux forum*



Onwards,

Q-
 
Sam, one question. What does this do?


Code:
   man ksh
   man chmod
   man crontab
   man cron
   man ftp

Thanks.


Onwards,

Q-
 
It should give you the manual pages for the commands [tt]ksh[/tt], [tt]chmod[/tt], [tt]crontab[/tt], [tt]cron[/tt] and [tt]ftp[/tt].

//Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top