Version 10 (modified by mitty, 15 years ago) (diff) |
---|
遅延実行
- cronスクリプトをバックグラウンドで実行するときの注意点 - ハッカー現役続行
- これを解決するには、このプロセスがopenしているファイルディスクリプタをcloseします。
sleep 15 >/dev/null 2>&1 &
- これを解決するには、このプロセスがopenしているファイルディスクリプタをcloseします。
問題
- 次のようなタスクを登録した場合
- /etc/cron.hourly/1
#!/bin/sh sleep 60 touch /tmp/1.$$.`date +%T.%N` ls -l /tmp/1*
- /etc/cron.hourly/2
#!/bin/sh sleep 60 touch /tmp/2.$$.`date +%T.%N` ls -l /tmp/2*
- /etc/cron.hourly/3
#!/bin/sh sleep 60 touch /tmp/3.$$.`date +%T.%N` ls -l /tmp/3*
- /etc/cron.hourly/1
- 1 -> 2 -> 3 と逐次実行されるため、同時並行より遅いタイミングでタスクが処理される
- pstree -ap
├─cron,3751 │ └─cron,3792 │ └─sh,3794 -c cd / && run-parts --report /etc/cron.hourly │ └─run-parts,3795 --report /etc/cron.hourly │ └─1,3796 /etc/cron.hourly/1 │ └─sleep,3797 60
- pstree -ap
├─cron,3751 │ └─cron,3792 │ ├─sendmail,3806 -i -FCronDaemon -oem root │ │ └─postdrop,3809 -r │ └─sh,3794 -c cd / && run-parts --report /etc/cron.hourly │ └─run-parts,3795 --report /etc/cron.hourly │ └─2,3807 /etc/cron.hourly/2 │ └─sleep,3808 60
- pstree -ap
├─cron,3751 │ └─cron,3792 │ ├─sendmail,3806 -i -FCronDaemon -oem root │ │ └─postdrop,3809 -r │ └─sh,3794 -c cd / && run-parts --report /etc/cron.hourly │ └─run-parts,3795 --report /etc/cron.hourly │ └─3,3816 /etc/cron.hourly/3 │ └─sleep,3817 60
- pstree -ap
- 実行ログはまとめて配送される
- mail
Mail version 8.1.2 01/15/2001. Type ? for help. "/var/mail/mitty": 1 message 1 new >N 1 root@lucid.mitty. Tue May 18 17:20 24/968 Cron <root@lucid> cd / & & Message 1: From root@lucid.mitty.jp Tue May 18 17:20:01 2010 X-Original-To: root From: root@lucid.mitty.jp (Cron Daemon) To: root@lucid.mitty.jp Subject: Cron <root@lucid> cd / && run-parts --report /etc/cron.hourly Content-Type: text/plain; charset=ANSI_X3.4-1968 X-Cron-Env: <SHELL=/bin/sh> X-Cron-Env: <PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin> X-Cron-Env: <HOME=/root> X-Cron-Env: <LOGNAME=root> Date: Tue, 18 May 2010 17:18:01 +0900 (JST) /etc/cron.hourly/1: -rw-r--r-- 1 root root 0 May 18 17:18 /tmp/1.3796.17:18:01.530794512 /etc/cron.hourly/2: -rw-r--r-- 1 root root 0 May 18 17:19 /tmp/2.3807.17:19:01.552276713 /etc/cron.hourly/3: -rw-r--r-- 1 root root 0 May 18 17:20 /tmp/3.3816.17:20:01.573785281
- mail
サブシェルにより解決法
- 各タスクを次のようにサブシェル化する
- /etc/cron.hourly/1
#!/bin/sh ( sleep 60 touch /tmp/1.$$.`date +%T.%N` ls -l /tmp/1* 2>&1 | mail $LOGNAME ) >/dev/null 2>&1 &
- /etc/cron.hourly/2
#!/bin/sh ( sleep 60 touch /tmp/2.$$.`date +%T.%N` ls -l /tmp/2* 2>&1 | mail $LOGNAME ) >/dev/null 2>&1 &
- /etc/cron.hourly/3
#!/bin/sh ( sleep 60 touch /tmp/3.$$.`date +%T.%N` ls -l /tmp/3* 2>&1 | mail $LOGNAME ) >/dev/null 2>&1 &
- /etc/cron.hourly/1
- 同時実行される
- pstree -ap
├─1,3860 /etc/cron.hourly/1 │ └─sleep,3865 60 ├─2,3862 /etc/cron.hourly/2 │ └─sleep,3864 60 ├─3,3866 /etc/cron.hourly/3 │ └─sleep,3867 60 ├─cron,3836
- pstree -ap
- 実行ログはmailコマンドにより個別に配送される
- mail
Mail version 8.1.2 01/15/2001. Type ? for help. "/var/mail/mitty": 3 messages 3 new >N 1 root@lucid.mitty. Tue May 18 17:18 13/480 N 2 root@lucid.mitty. Tue May 18 17:18 13/480 N 3 root@lucid.mitty. Tue May 18 17:18 13/480 & Message 1: From root@lucid.mitty.jp Tue May 18 17:18:01 2010 X-Original-To: root To: root@lucid.mitty.jp Date: Tue, 18 May 2010 17:18:01 +0900 (JST) From: root@lucid.mitty.jp (root) -rw-r--r-- 1 root root 0 May 18 17:18 /tmp/3.3863.17:18:01.400271817 & Message 2: From root@lucid.mitty.jp Tue May 18 17:18:01 2010 X-Original-To: root To: root@lucid.mitty.jp Date: Tue, 18 May 2010 17:18:01 +0900 (JST) From: root@lucid.mitty.jp (root) -rw-r--r-- 1 root root 0 May 18 17:18 /tmp/2.3861.17:18:01.397221662 & Message 3: From root@lucid.mitty.jp Tue May 18 17:18:01 2010 X-Original-To: root To: root@lucid.mitty.jp Date: Tue, 18 May 2010 17:18:01 +0900 (JST) From: root@lucid.mitty.jp (root) -rw-r--r-- 1 root root 0 May 18 17:18 /tmp/1.3859.17:18:01.401215856
- mail
実行サンプル
- /etc/cron.hourly/crontest
#!/bin/sh ( /bin/df -h 2>&1 | mail -E -s "Cron <$LOGNAME@`/bin/hostname`> $0" $LOGNAME ) >/dev/null 2>&1 &
- mail
Mail version 8.1.2 01/15/2001. Type ? for help. "/var/mail/mitty": 1 message 1 new >N 1 root@lucid.mitty. Wed May 19 10:17 21/903 Cron <root@lucid> /etc/cron & Message 1: From root@lucid.mitty.jp Wed May 19 10:17:01 2010 X-Original-To: root To: root@lucid.mitty.jp Subject: Cron <root@lucid> /etc/cron.hourly/crontest Date: Wed, 19 May 2010 10:17:01 +0900 (JST) From: root@lucid.mitty.jp (root) Filesystem Size Used Avail Use% Mounted on /dev/sda1 3.8G 903M 2.9G 24% / none 243M 168K 243M 1% /dev none 247M 0 247M 0% /dev/shm none 247M 40K 247M 1% /var/run none 247M 0 247M 0% /var/lock none 247M 0 247M 0% /lib/init/rw none 3.8G 903M 2.9G 24% /var/lib/ureadahead/debugfs
遅延実行テスト
- crontab -l
*/2 * * * * /home/mitty/crontest
フォアグラウンド sleep
- /home/mitty/crontest
#!/bin/sh export LANG=C touch /tmp/$$.`date +%T` ls -l /tmp sleep 60; touch /tmp/$$.`date +%T` ls -l /tmp
- pstree -ap
├─cron,662 │ └─cron,2325 │ ├─sendmail,2332 -i -FCronDaemon -oem mitty │ │ └─postdrop,2333 -r │ └─sh,2326 -c /home/mitty/crontest │ └─crontest,2327 /home/mitty/crontest │ └─sleep,2331 60
- mail
Mail version 8.1.2 01/15/2001. Type ? for help. "/var/mail/mitty": 1 message 1 new >N 1 root@lucid.mitty. Tue May 18 16:07 25/937 Cron <mitty@lucid> /home/mi & Message 1: From mitty@lucid.mitty.jp Tue May 18 16:07:01 2010 X-Original-To: mitty From: root@lucid.mitty.jp (Cron Daemon) To: mitty@lucid.mitty.jp Subject: Cron <mitty@lucid> /home/mitty/crontest Content-Type: text/plain; charset=ANSI_X3.4-1968 X-Cron-Env: <SHELL=/bin/sh> X-Cron-Env: <HOME=/home/mitty> X-Cron-Env: <PATH=/usr/bin:/bin> X-Cron-Env: <LOGNAME=mitty> Date: Tue, 18 May 2010 16:06:01 +0900 (JST) total 0 -rw-r--r-- 1 mitty mitty 0 May 18 16:06 2327.16:06:01 drwx------ 2 mitty mitty 22 May 18 13:51 ssh-qAiiolp854 total 0 -rw-r--r-- 1 mitty mitty 0 May 18 16:06 2327.16:06:01 -rw-r--r-- 1 mitty mitty 0 May 18 16:07 2327.16:07:01 drwx------ 2 mitty mitty 22 May 18 13:51 ssh-qAiiolp854
with mail command
- /home/mitty/crontest
#!/bin/sh export LANG=C touch /tmp/$$.`date +%T` ls -l /tmp 2>&1 | mail $LOGNAME sleep 60; touch /tmp/$$.`date +%T` ls -l /tmp 2>&1 | mail $LOGNAME
- pstree -ap
├─cron,662 │ └─cron,2647 │ └─sh,2649 -c /home/mitty/crontest │ └─crontest,2650 /home/mitty/crontest │ └─sleep,2658 60
- mail
Mail version 8.1.2 01/15/2001. Type ? for help. "/var/mail/mitty": 2 messages 2 new >N 1 mitty@lucid.mitty Tue May 18 16:30 15/540 N 2 mitty@lucid.mitty Tue May 18 16:31 16/595 & Message 1: From mitty@lucid.mitty.jp Tue May 18 16:30:01 2010 X-Original-To: mitty To: mitty@lucid.mitty.jp Date: Tue, 18 May 2010 16:30:01 +0900 (JST) From: mitty@lucid.mitty.jp (mitty) total 0 -rw-r--r-- 1 mitty mitty 0 May 18 16:30 2650.16:30:01 drwx------ 2 mitty mitty 22 May 18 13:51 ssh-qAiiolp854 & Message 2: From mitty@lucid.mitty.jp Tue May 18 16:31:02 2010 X-Original-To: mitty To: mitty@lucid.mitty.jp Date: Tue, 18 May 2010 16:31:01 +0900 (JST) From: mitty@lucid.mitty.jp (mitty) total 0 -rw-r--r-- 1 mitty mitty 0 May 18 16:30 2650.16:30:01 -rw-r--r-- 1 mitty mitty 0 May 18 16:31 2650.16:31:01 drwx------ 2 mitty mitty 22 May 18 13:51 ssh-qAiiolp854
バックグラウンド sleep
- /home/mitty/crontest
#!/bin/sh export LANG=C touch /tmp/cron.$$.`date +%T.%N` ls -l /tmp sleep 60 & touch /tmp/cron.$$.`date +%T.%N` ls -l /tmp
- pstree -ap
├─cron,662 │ └─cron,2948 │ ├─sendmail,2955 -i -FCronDaemon -oem mitty │ │ └─postdrop,2959 -r │ └─(sh,2950) ├─sleep,2956 60
- mail
Mail version 8.1.2 01/15/2001. Type ? for help. "/var/mail/mitty": 1 message 1 new >N 1 root@lucid.mitty. Tue May 18 16:53 25/982 Cron <mitty@lucid> /home/mi & Message 1: From mitty@lucid.mitty.jp Tue May 18 16:53:02 2010 X-Original-To: mitty From: root@lucid.mitty.jp (Cron Daemon) To: mitty@lucid.mitty.jp Subject: Cron <mitty@lucid> /home/mitty/crontest Content-Type: text/plain; charset=ANSI_X3.4-1968 X-Cron-Env: <SHELL=/bin/sh> X-Cron-Env: <HOME=/home/mitty> X-Cron-Env: <PATH=/usr/bin:/bin> X-Cron-Env: <LOGNAME=mitty> Date: Tue, 18 May 2010 16:52:02 +0900 (JST) total 0 -rw-r--r-- 1 mitty mitty 0 May 18 16:52 cron.2951.16:52:01.997709994 drwx------ 2 mitty mitty 22 May 18 13:51 ssh-qAiiolp854 total 0 -rw-r--r-- 1 mitty mitty 0 May 18 16:52 cron.2951.16:52:01.997709994 -rw-r--r-- 1 mitty mitty 0 May 18 16:52 cron.2951.16:52:02.013689211 drwx------ 2 mitty mitty 22 May 18 13:51 ssh-qAiiolp854
with mail command
- /home/mitty/crontest
#!/bin/sh export LANG=C touch /tmp/cron.$$.`date +%T.%N` ls -l /tmp 2>&1 | mail $LOGNAME sleep 60 & touch /tmp/cron.$$.`date +%T.%N` ls -l /tmp 2>&1 | mail $LOGNAME
- pstree -ap
├─cron,662 │ └─cron,3006 │ └─(sh,3008) ├─sleep,3017 60
- mail
Mail version 8.1.2 01/15/2001. Type ? for help. "/var/mail/mitty": 2 messages 2 new >N 1 mitty@lucid.mitty Tue May 18 16:56 15/555 N 2 mitty@lucid.mitty Tue May 18 16:56 16/625 & Message 1: From mitty@lucid.mitty.jp Tue May 18 16:56:01 2010 X-Original-To: mitty To: mitty@lucid.mitty.jp Date: Tue, 18 May 2010 16:56:01 +0900 (JST) From: mitty@lucid.mitty.jp (mitty) total 0 -rw-r--r-- 1 mitty mitty 0 May 18 16:56 cron.3009.16:56:01.044354156 drwx------ 2 mitty mitty 22 May 18 13:51 ssh-qAiiolp854 & Message 2: From mitty@lucid.mitty.jp Tue May 18 16:56:01 2010 X-Original-To: mitty To: mitty@lucid.mitty.jp Date: Tue, 18 May 2010 16:56:01 +0900 (JST) From: mitty@lucid.mitty.jp (mitty) total 0 -rw-r--r-- 1 mitty mitty 0 May 18 16:56 cron.3009.16:56:01.044354156 -rw-r--r-- 1 mitty mitty 0 May 18 16:56 cron.3009.16:56:01.089449007 drwx------ 2 mitty mitty 22 May 18 13:51 ssh-qAiiolp854
>/dev/null 2>&1 &
- /home/mitty/crontest
#!/bin/sh export LANG=C touch /tmp/cron.$$.`date +%T.%N` ls -l /tmp 2>&1 | mail $LOGNAME sleep 60 >/dev/null 2>&1 & touch /tmp/cron.$$.`date +%T.%N` ls -l /tmp 2>&1 | mail $LOGNAME
- pstree -ap
├─cron,662 ├─sleep,3083 60
- mail
Mail version 8.1.2 01/15/2001. Type ? for help. "/var/mail/mitty": 2 messages 2 new >N 1 mitty@lucid.mitty Tue May 18 17:00 15/555 N 2 mitty@lucid.mitty Tue May 18 17:00 16/625 & Message 1: From mitty@lucid.mitty.jp Tue May 18 17:00:01 2010 X-Original-To: mitty To: mitty@lucid.mitty.jp Date: Tue, 18 May 2010 17:00:01 +0900 (JST) From: mitty@lucid.mitty.jp (mitty) total 0 -rw-r--r-- 1 mitty mitty 0 May 18 17:00 cron.3075.17:00:01.108414369 drwx------ 2 mitty mitty 22 May 18 13:51 ssh-qAiiolp854 & Message 2: From mitty@lucid.mitty.jp Tue May 18 17:00:01 2010 X-Original-To: mitty To: mitty@lucid.mitty.jp Date: Tue, 18 May 2010 17:00:01 +0900 (JST) From: mitty@lucid.mitty.jp (mitty) total 0 -rw-r--r-- 1 mitty mitty 0 May 18 17:00 cron.3075.17:00:01.108414369 -rw-r--r-- 1 mitty mitty 0 May 18 17:00 cron.3075.17:00:01.161439061 drwx------ 2 mitty mitty 22 May 18 13:51 ssh-qAiiolp854
サブシェル
- /home/mitty/crontest
#!/bin/sh export LANG=C ( touch /tmp/cron.$$.`date +%T.%N` ls -l /tmp 2>&1 | mail $LOGNAME sleep 60 touch /tmp/cron.$$.`date +%T.%N` ls -l /tmp 2>&1 | mail $LOGNAME ) >/dev/null 2>&1 &
- pstree -ap
├─cron,662 ├─crontest,3343 /home/mitty/crontest │ └─sleep,3351 60
- mail
Mail version 8.1.2 01/15/2001. Type ? for help. "/var/mail/mitty": 2 messages 2 new >N 1 mitty@lucid.mitty Tue May 18 17:24 15/555 N 2 mitty@lucid.mitty Tue May 18 17:25 16/625 & Message 1: From mitty@lucid.mitty.jp Tue May 18 17:24:01 2010 X-Original-To: mitty To: mitty@lucid.mitty.jp Date: Tue, 18 May 2010 17:24:01 +0900 (JST) From: mitty@lucid.mitty.jp (mitty) total 0 -rw-r--r-- 1 mitty mitty 0 May 18 17:24 cron.3342.17:24:01.565319416 drwx------ 2 mitty mitty 22 May 18 13:51 ssh-qAiiolp854 & Message 2: From mitty@lucid.mitty.jp Tue May 18 17:25:01 2010 X-Original-To: mitty To: mitty@lucid.mitty.jp Date: Tue, 18 May 2010 17:25:01 +0900 (JST) From: mitty@lucid.mitty.jp (mitty) total 0 -rw-r--r-- 1 mitty mitty 0 May 18 17:24 cron.3342.17:24:01.565319416 -rw-r--r-- 1 mitty mitty 0 May 18 17:25 cron.3342.17:25:01.614828392 drwx------ 2 mitty mitty 22 May 18 13:51 ssh-qAiiolp854
環境変数
Ubuntu lucid
normal user task
- crontab -l
* * * * * /home/mitty/test
- cat /home/mitty/test
#!/bin/sh export
- mail
From mitty@lucid.mitty.jp Tue May 18 16:25:01 2010 X-Original-To: mitty From: root@lucid.mitty.jp (Cron Daemon) To: mitty@lucid.mitty.jp Subject: Cron <mitty@lucid> /home/mitty/test Content-Type: text/plain; charset=ANSI_X3.4-1968 X-Cron-Env: <SHELL=/bin/sh> X-Cron-Env: <HOME=/home/mitty> X-Cron-Env: <PATH=/usr/bin:/bin> X-Cron-Env: <LOGNAME=mitty> Date: Tue, 18 May 2010 16:25:01 +0900 (JST) export HOME='/home/mitty' export LOGNAME='mitty' export PATH='/usr/bin:/bin' export PWD='/home/mitty' export SHELL='/bin/sh'
system task
- cat /etc/cron.hourly/test
#!/bin/sh export
- mail
From root@lucid.mitty.jp Tue May 18 17:17:01 2010 X-Original-To: root From: root@lucid.mitty.jp (Cron Daemon) To: root@lucid.mitty.jp Subject: Cron <root@lucid> cd / && run-parts --report /etc/cron.hourly Content-Type: text/plain; charset=ANSI_X3.4-1968 X-Cron-Env: <SHELL=/bin/sh> X-Cron-Env: <PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin> X-Cron-Env: <HOME=/root> X-Cron-Env: <LOGNAME=root> Date: Tue, 18 May 2010 17:17:01 +0900 (JST) /etc/cron.hourly/test: export HOME='/root' export LOGNAME='root' export OLDPWD='/root' export PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin' export PWD='/' export SHELL='/bin/sh'