Linux Shell : crontab, at, &
Sometimes, we need to run the processes in the background.The following four command are useful : crontab, at, & and nohup.
crontab syntax:
# crontab [-u user] -e -l -r
-u : user
-e : edit or modify the crontab file
-l : list the crontab file contents.
-r : delete the crontab file crontab is schedule periodic background work,and it maintain the crontab files for individual users.
Every user could has his own crontab file, but in most case, system administrator forbids this operation by modifying ‘cron.deny’ and ‘cron.allow’ file, and the sa will keep only one crontab file for all users.
In the crontab tables,the meaning of every column are showed here:
The 1st column : miniute (1-59)
The 2nd column: hour (1-23,and 0 indicates middle night )
The 3rd column: date (1-31)
The 4th column: month (1-12)
The 5th column: week (0-6, o is sunday)
The 6th column: command will be executed (Note,absolute directory must be added)
eg:
10 9 24 11 4 /sbin/init 6
This string means the system will reboot at November(11) 24,9:10,Thursday.
A useful command:
# ps -x # ps -fe
-x : processes w/o controlling ttys.
-e : all processes
-f : output format
when I issued ‘ps -x’ command, I found there was such a line showing in the screen:
...... 5942 ? S 0:07 kmail -caption KMail -icon kmail -miniicon kmail ......
So,I wanted to make the kmail processes startup automatic when the user logs in. I modified the crontab files by adding the following lines:
.... 32 11 24 11 4 /opt/kde3/bin/kmail ....
And waited for the action happening,but at the 11:10, SuSE told me that I had gotten a mail in the /var/spool/mail/root which said ‘ERROR:kUnique Application,Can’t determing DISPLAY Aborting’. Oh, what’s the matter?
I added an other line indicating the display parameter before the kmail command in crontab file:
10 11 24 11 4 export DISPLAY=0:0 ; /opt/kde3/bin/kmail
But I still received an error report in /var/spool/mail/root:
----------/var/spool/mail/root--------------- From root@linux.site Thu Nov 24 11:32:21 2005 Return-Path: <root@linux.site> X-Original-To: root Delivered-To: root@linux.site Received: by linux.site (Postfix, from userid 0) id C017E101213; Thu, 24 Nov 2005 11:32:21 +0800 (CST) From: root@linux.site (Cron Daemon) To: root@linux.site Subject: Cron <root@linux> export DISPLAY=0:0 ; /opt/kde3/bin/kmail X-Cron-Env: <SHELL=/bin/sh> X-Cron-Env: <HOME=/root> X-Cron-Env: <PATH=/usr/bin:/bin> X-Cron-Env: <LOGNAME=root> Message-Id: <20051124033221.C017E101213@linux.site> Date: Thu, 24 Nov 2005 11:32:21 +0800 (CST) _X11TransSocketINETConnect() can't get address for 0:6000: Name or service not known kded: cannot connect to X server 0:0 DCOP aborting call from 'anonymous-5910' to 'kded' kded: ERROR: Communication problem with kded, it probably crashed. _X11TransSocketINETConnect() can't get address for 0:6000: Name or service not known kdeinit: Can't connect to the X Server. kdeinit: Might not terminate at end of session. _X11TransSocketINETConnect() can't get address for 0:6000: Name or service not known kmail: cannot connect to X server 0:0 DCOP aborting call from 'anonymous-5901' to 'kmail' ERROR: Communication problem with kmail, it probably crashed. Mutex destroy failure: Device or resource busy sh: line 1: iceauth: command not found
The test failed. I’ll try to resolve this problem later.Maybe using pipe is a good idea.Waiting…
at syntax: at [-f script] [-m -l -r] [time] [date]
-f script : Shell script or command
-m : mail the report to user
-l : list the quote contains waiting jobs
-r : cancel the job by using its job-ID
time : H,HH,HHMM,HH:MM,H:M
date : today,tomorrow
at has two mode: command line and prompt mode.
Command line mode,for example:
$ at 3.00 pm tomorrow -f /apps/bin/db-table.sh Prompt mode: $ at 14:08 at> find / -name 'adobe' > report.txt at> [EOT]
[EOT],NOT IS EOF,means “CTRL+D” to save and quit the at prompt mode. Note,once the job was submitted, at will store the running environment including path.but Using crontab command,we must identify the absolute directory.
& character : command &
& is used to make the job running in the background.These jobs may be find,print,soft,Shell Script and other high-costing issues.The interactive of user is forbidden,or it will confuse the system to make it waiting forever. eg:
$ find -name 'adobe' & [1] 6531 linux $ ps -e PID TTY TIME CMD ... 6510 tty2 00:00:00 bash 6532 tty2 00:00:00 ps [1]+ Done ...... find -name 'adobe'