1846267 Members
5274 Online
110256 Solutions
New Discussion

cron task

 
mtang
Occasional Contributor

cron task

I want to submit task by crontab -e, when the command exceed one command line, I try to use % to expand the command to the next line. But doesn't work. Anyone can provide a sample cron file which has the command over one line.
10 REPLIES 10
RAC_1
Honored Contributor

Re: cron task

You can continue a line as follows.

xxxx ssjkjj skjjw;jw l;wk;kw \
sssjsj

That counts as one line. Also it is a bad practice to do crontab -e. It is better to
do crontab -l "user_name" > /tmp/somefile.

Modify /tmp/somefile.

crontab /tmp/somefile

Anil
There is no substitute to HARDWORK
Simon Hargrave
Honored Contributor

Re: cron task

Not sure if you can do it, but even if you could I wouldn't.

If you need complicated scripting, put it in a separate script and call that.

If you just need to run a few commands and capture their output together use something like:

* * * * * ( command1 ; command2 ; command3 ) > logfile 2>&1
DCE
Honored Contributor

Re: cron task

RAC's suggestion of placing a backslash (\) where you want to break the command across lines works fine. I have used with several commands in the past with no problem.
Ermin Borovac
Honored Contributor

Re: cron task

Percent character (%) is crontab is used as follows

* * * * * /usr/bin/mailx -s test %hello world

is the same as

* * * * * echo "hello world" | /usr/bin/mailx -s test

So all text up to % in the command field is executed by the shell. The rest (after %) is provided as standard input for the command that appears before %.
Bill Hassell
Honored Contributor

Re: cron task

cron (like the shell and virtually all Unix commands) do not care about the line length. What you see on your screen in vi is artificially folded by the terminal as well as vi. You can type a command that is thousands of characters long (make sure the option for wrapmargin is 0 or vi will automatically break long lines into separate lines. But no problem: just put the cursor on the first line and type J (capital J) and it will join the next line to the current line. Repeat as need for one long line. To see that it is all one line, type $ and the cursor will jump to the end of the line (which might several terminal lines down the page).

(to turn off wrapmargin, type the command :set wm=0


Bill Hassell, sysadmin
Jino.P.V
Frequent Advisor

Re: cron task

Hi,
I have just tried. It is taking as one line only. I have typed without giving "enter" in between. I don't know is it because WM mentioned by Bill is set by default in my terminal as I am using reflection to connect to all the servers. So you need not to use different charecters to make it one line.

regards,

Jino
Bill Hassell
Honored Contributor

Re: cron task

You can check on all your settings in vi with the ex commend:

:set
autoindent autowrite ignorecase redraw report=1 shell=/sbin/sh showmode term=70092 nowrapscan wrapmargin=2

In my case, the automatic line split occurs when typing a new line and I reach the last 2 columns. An existing long line is not split automatically by vi. Set wrapmargin (or wm, the abbreviation) to zero and you'll get long lines by default. To make this permanent for your shell, edit .exrc in your home directory to add this line at the top:

set nowrapsearch ignorecase autoindent autowrite wrapmargin=0 sh=/usr/bin/sh report=1 showmode

Now if you don't have a .exrc file, just create it with this above line. If you already have one, just adjust the existing set line similar to above. I have several useful options in the above example. The () parens show the abbreviation you can use for the same setting. All of these can be set inside vi for the current session using :set

nowrapsearch (nows) stops vi from silently wrapping around the file when searching.

ignorecase (ic) makes searches case-insensitive

autoindent (ai) will automatically follow your indentation. Each new line starts directly below the previous line including and leading spaces. To back up or get back to the beginning of the line, type CTRL-d to backup 1 tab distance.

autowrite (aw) will post the current changes to the original file when you escape to a shell (ie, :!sh)

wrapmargin=0 (wm=0) says to not split new lines when they are within XX columns of the end of the margin. NOTE: wm=72 means split lines starting 72 columns BEFORE the right edge, not column 72. This setting allows the wm setting to vary based on the width of the terminal.

sh=/usr/bin/sh sets the shell to use. If not set, the SHELL variable is used.

report=1 sets the number of lines that must be changed by a command before a report line is displayed (ie, 12 lines deleted) at the bottom left of the screen.


showmode (smd) sets the tag message at the bottom right of your vi screen to show REPLACE or INPUT when vi is not in the command mode.

All these (and many more) settings are defined in the man page for ex (not vi). Vi is actually several editors in one. All : commands (like :!sh or :%d or :set) are run byy the ex editor.


Bill Hassell, sysadmin
Geoff Wild
Honored Contributor

Re: cron task

To make your crontab more "readable" I would put long commands in a script instead...then call the script from cron.

Example:

#defrag filesystems
0 22 * * 4 /usr/local/bin/fsadm.defrag >/dev/null 2>&1


# cat /usr/local/bin/fsadm.defrag
#!/bin/sh
# defrag all mounted file systems
LOG=/tmp/fsadm.defrag.log
if [ -f $LOG ]
then
mv $LOG $LOG.old
fi
cat /dev/null > $LOG

for i in `mount -l | grep -v stand |awk '{print $1}'`
do
echo "defraging " $i >> $LOG
fsadm -F vxfs -d -D -e -E $i >> $LOG
done


It would be ugly to put that all on a single command line...

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Bill Hassell
Honored Contributor

Re: cron task

Geoff wrote...

>> for i in `mount -l | grep -v stand |awk '{print $1}'`

mount -l shows local filesystems but will return vxfs (desired), hfs (not applicable) and cdfs (not applicable). If you change the command to something like this, it will work for all systems:

for i in $(bdf -t vxfs | grep -v %used |awk '{print $1}')


Bill Hassell, sysadmin
Geoff Wild
Honored Contributor

Re: cron task

Bill - pretty cool - except that bdf doesn't format nicely - when filesystems or mount points have long names:

# bdf -t vxfs | grep -v %used |awk '{print $1}'
/dev/vg00/lvol3
/dev/vg00/lvol8
/dev/vg48/lvsw
/dev/vg48/lvoraclesw
20971520
/dev/vg48/lvmiscsw
/dev/vg48/lvibmsw
/dev/vg48/lvhpsw
/dev/vg48/lvemcsw
/dev/vg46/lvol1
/dev/vg46/lvol2
/dev/vg00/lvol9
/dev/vg00/lvol7
/dev/vg47/lvol1
/dev/vg00/lvol6
/dev/vg14/lvol2
/dev/vg14/lvol5
/dev/vg14/lvol11
/dev/vg14/lvol1
/dev/vg14/lvol6
/dev/vg14/lvol3
/dev/vg14/lvol4
/dev/vg14/lvol10
/dev/vg00/lvol5
/dev/vg47/lvol2
/dev/vg00/lvol4
/dev/vg14/lvol7

Maybe use the bdfmegs script?

# bdfmegs vxfs | grep -v File |awk '{print $1}'
/dev/vg00/lvol3
/dev/vg00/lvol1
/dev/vg00/lvol8
/dev/vg48/lvsw
/dev/vg48/lvoraclesw
/dev/vg48/lvmiscsw
/dev/vg48/lvibmsw
/dev/vg48/lvhpsw
/dev/vg48/lvemcsw
/dev/vg46/lvol1
/dev/vg46/lvol2
/dev/vg00/lvol9
/dev/vg00/lvol7
/dev/vg47/lvol1
/dev/vg00/lvol6
/dev/vg14/lvol2
/dev/vg14/lvol5
/dev/vg14/lvol11
/dev/vg14/lvol1
/dev/vg14/lvol6
/dev/vg14/lvol3
/dev/vg14/lvol4
/dev/vg14/lvol10
/dev/vg00/lvol5
/dev/vg47/lvol2
/dev/vg00/lvol4
/dev/vg14/lvol7

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.