미니옵빠의 code stubs
crontab 본문
출처: http://www.debianhelp.co.uk/crontab.htm
Run programs at startup via crontab
There is an easy way to start a program during system boot. Just put this in your crontab:
@reboot /path/to/my/program
The command will be executed on every (re)boot. Crontab can be modified by running
#crontab -e
Other available Options
string meaning
------ -----------
@reboot Run once, at startup.
@yearly Run once a year, "0 0 1 1 *".
@annually (same as @yearly)
@monthly Run once a month, "0 0 1 * *".
@weekly Run once a week, "0 0 * * 0".
@daily Run once a day, "0 0 * * *".
@midnight (same as @daily)
@hourly Run once an hour, "0 * * * *".
More information about crontab options is available in the man page check here
How to Use percentage sign (%) in a crontab entry
Usually, a % is used to denote a new line in a crontab entry. The first % is special in that it denotes the start of STDIN for the crontab entry's command. A trivial example is:
* * * * * cat - % another minute has passed
This would output the text
another minute has passed
After the first %, all other %s in a crontab entry indicate a new line. So a slightly different trivial example is:
* * * * * cat - % another % minute % has % passed
This would output the text
another
minute
has
passed
Note how the % has been used to indicate a new line.
The problem is how to use a % in a crontab line to as a % and not as a new line. Many manuals will say escape it with a \. This certainly stops its interpretation as a new line but the shell running the cron job can leave the \ in. For example:
* * * * * echo '\% another \% minute \% has \% passed'
would output the text
\% another \% minute \% has \% passed
Clearly, not what was intended.
A solution is to pass the text through sed. The crontab example now becomes:
* * * * * echo '\% another \% minute \% has \% passed'| sed -e 's|\\||g'
This would output the text
% another % minute % has % passed
which is what was intended.
This technique is very useful when using a MySQL command within a crontab. MySQL command can often have a % in them. Some example are:
SET @monyy=DATE_FORMAT(NOW(),"%M %Y")
SELECT * FROM table WHERE name LIKE 'fred%'
So, to have a crontab entry to run the MySQL command
mysql -vv -e "SELECT * FROM table WHERE name LIKE Fred%'" member_list
would have to appear in the crontab as
echo "SELECT * FROM table WHERE name LIKE 'Fred\%'" | sed -e 's|\\||g' | mysql -vv member_list
Pulling the crontab entry apart there is:
the echo command sends the MySQL command to STDOUT where it is piped into
sed which removes any back slashes before sending the output to STDOUT where it is piped into
the mysql command processor which reads its commands from STDIN