Home  >  Article  >  Operation and Maintenance  >  Do you know what the special meaning of % in crontab under Linux is?

Do you know what the special meaning of % in crontab under Linux is?

藏色散人
藏色散人forward
2021-09-06 17:26:012147browse

The following column linux system tutorial will introduce to you the use of % in crontab under Linux and its special meaning. I hope it will be helpful to friends in need!

Do you know what the special meaning of % in crontab under Linux is?

% usage in crontab under Linux

The following script in crontab cannot

 0 1 * * *   (cd /u01/prod; rsync -avz app 192.168.0.192:/u01/prod/) &>/home/applprod/backuplog/rsync_`date +%Y%m%d_%H%M%S`.log

During execution, the /var/log/messages log displays:

Sep 22 22:50:01 ebsapp CROND[13389]: (applprod) CMD ((cd /u01/prod; rsync -avz app 192.168.0.192:/u01/prod/) &>/home/applprod/backuplog/rsync_`date +)

It seems that the command has been truncated.

So I encapsulated all the commands to be executed into a script and put them in crontab for execution.

But when I checked the crontab manpage later, I found that % has a special meaning in crontab:

The  "sixth"  field (the rest of the line) specifies the command to be run.  The entire command portion of the line, up to a new-
       line or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile.   Percent-signs
       (%)  in  the  command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first %
       will be sent to the command as standard input.

The problem is now clear. It is not that the crontab command is truncated, but is understood as There is another meaning, and the solution is to escape %. The modified script becomes:

03 23 * * * (cd /u01/prod; rsync -avz app 192.168.0.192:/u01/prod/) &>/home/applprod/backuplog/rsync_`date +\%Y\%m\%d_\%H\%M\%S`.log

The above is the detailed content of Do you know what the special meaning of % in crontab under Linux is?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete