| 11 | }}} |
| 12 | |
| 13 | == 問題 == |
| 14 | * 次のようなタスクを登録した場合 |
| 15 | 1. /etc/cron.hourly/1 |
| 16 | {{{ |
| 17 | #!/bin/sh |
| 18 | |
| 19 | sleep 60 |
| 20 | touch /tmp/1.$$.`date +%T.%N` |
| 21 | ls -l /tmp/1* |
| 22 | }}} |
| 23 | 1. /etc/cron.hourly/2 |
| 24 | {{{ |
| 25 | #!/bin/sh |
| 26 | |
| 27 | sleep 60 |
| 28 | touch /tmp/2.$$.`date +%T.%N` |
| 29 | ls -l /tmp/2* |
| 30 | }}} |
| 31 | 1. /etc/cron.hourly/3 |
| 32 | {{{ |
| 33 | #!/bin/sh |
| 34 | |
| 35 | sleep 60 |
| 36 | touch /tmp/3.$$.`date +%T.%N` |
| 37 | ls -l /tmp/3* |
| 38 | }}} |
| 39 | |
| 40 | * 1 -> 2 -> 3 と逐次実行されるため、同時並行より遅いタイミングでタスクが処理される |
| 41 | 1. pstree -ap |
| 42 | {{{ |
| 43 | ├─cron,3751 |
| 44 | │ └─cron,3792 |
| 45 | │ └─sh,3794 -c cd / && run-parts --report /etc/cron.hourly |
| 46 | │ └─run-parts,3795 --report /etc/cron.hourly |
| 47 | │ └─1,3796 /etc/cron.hourly/1 |
| 48 | │ └─sleep,3797 60 |
| 49 | }}} |
| 50 | 1. pstree -ap |
| 51 | {{{ |
| 52 | ├─cron,3751 |
| 53 | │ └─cron,3792 |
| 54 | │ ├─sendmail,3806 -i -FCronDaemon -oem root |
| 55 | │ │ └─postdrop,3809 -r |
| 56 | │ └─sh,3794 -c cd / && run-parts --report /etc/cron.hourly |
| 57 | │ └─run-parts,3795 --report /etc/cron.hourly |
| 58 | │ └─2,3807 /etc/cron.hourly/2 |
| 59 | │ └─sleep,3808 60 |
| 60 | }}} |
| 61 | 1. pstree -ap |
| 62 | {{{ |
| 63 | ├─cron,3751 |
| 64 | │ └─cron,3792 |
| 65 | │ ├─sendmail,3806 -i -FCronDaemon -oem root |
| 66 | │ │ └─postdrop,3809 -r |
| 67 | │ └─sh,3794 -c cd / && run-parts --report /etc/cron.hourly |
| 68 | │ └─run-parts,3795 --report /etc/cron.hourly |
| 69 | │ └─3,3816 /etc/cron.hourly/3 |
| 70 | │ └─sleep,3817 60 |
| 71 | }}} |
| 72 | |
| 73 | * 実行ログはまとめて配送される |
| 74 | * mail |
| 75 | {{{ |
| 76 | Mail version 8.1.2 01/15/2001. Type ? for help. |
| 77 | "/var/mail/mitty": 1 message 1 new |
| 78 | >N 1 root@lucid.mitty. Tue May 18 17:20 24/968 Cron <root@lucid> cd / & |
| 79 | & |
| 80 | Message 1: |
| 81 | From root@lucid.mitty.jp Tue May 18 17:20:01 2010 |
| 82 | X-Original-To: root |
| 83 | From: root@lucid.mitty.jp (Cron Daemon) |
| 84 | To: root@lucid.mitty.jp |
| 85 | Subject: Cron <root@lucid> cd / && run-parts --report /etc/cron.hourly |
| 86 | Content-Type: text/plain; charset=ANSI_X3.4-1968 |
| 87 | X-Cron-Env: <SHELL=/bin/sh> |
| 88 | X-Cron-Env: <PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin> |
| 89 | X-Cron-Env: <HOME=/root> |
| 90 | X-Cron-Env: <LOGNAME=root> |
| 91 | Date: Tue, 18 May 2010 17:18:01 +0900 (JST) |
| 92 | |
| 93 | /etc/cron.hourly/1: |
| 94 | -rw-r--r-- 1 root root 0 May 18 17:18 /tmp/1.3796.17:18:01.530794512 |
| 95 | /etc/cron.hourly/2: |
| 96 | -rw-r--r-- 1 root root 0 May 18 17:19 /tmp/2.3807.17:19:01.552276713 |
| 97 | /etc/cron.hourly/3: |
| 98 | -rw-r--r-- 1 root root 0 May 18 17:20 /tmp/3.3816.17:20:01.573785281 |
| 99 | }}} |
| 100 | |
| 101 | === サブシェルにより解決法 === |
| 102 | * 各タスクを次のようにサブシェル化する |
| 103 | 1. /etc/cron.hourly/1 |
| 104 | {{{ |
| 105 | #!/bin/sh |
| 106 | |
| 107 | ( |
| 108 | sleep 60 |
| 109 | touch /tmp/1.$$.`date +%T.%N` |
| 110 | ls -l /tmp/1* 2>&1 | mail $LOGNAME |
| 111 | ) >/dev/null 2>&1 & |
| 112 | }}} |
| 113 | 1. /etc/cron.hourly/2 |
| 114 | {{{ |
| 115 | #!/bin/sh |
| 116 | |
| 117 | ( |
| 118 | sleep 60 |
| 119 | touch /tmp/2.$$.`date +%T.%N` |
| 120 | ls -l /tmp/2* 2>&1 | mail $LOGNAME |
| 121 | ) >/dev/null 2>&1 & |
| 122 | }}} |
| 123 | 1. /etc/cron.hourly/3 |
| 124 | {{{ |
| 125 | #!/bin/sh |
| 126 | |
| 127 | ( |
| 128 | sleep 60 |
| 129 | touch /tmp/3.$$.`date +%T.%N` |
| 130 | ls -l /tmp/3* 2>&1 | mail $LOGNAME |
| 131 | ) >/dev/null 2>&1 & |
| 132 | }}} |
| 133 | |
| 134 | * 同時実行される |
| 135 | * pstree -ap |
| 136 | {{{ |
| 137 | ├─1,3860 /etc/cron.hourly/1 |
| 138 | │ └─sleep,3865 60 |
| 139 | ├─2,3862 /etc/cron.hourly/2 |
| 140 | │ └─sleep,3864 60 |
| 141 | ├─3,3866 /etc/cron.hourly/3 |
| 142 | │ └─sleep,3867 60 |
| 143 | |
| 144 | ├─cron,3836 |
| 145 | }}} |
| 146 | |
| 147 | * 実行ログはmailコマンドにより個別に配送される |
| 148 | * mail |
| 149 | {{{ |
| 150 | Mail version 8.1.2 01/15/2001. Type ? for help. |
| 151 | "/var/mail/mitty": 3 messages 3 new |
| 152 | >N 1 root@lucid.mitty. Tue May 18 17:18 13/480 |
| 153 | N 2 root@lucid.mitty. Tue May 18 17:18 13/480 |
| 154 | N 3 root@lucid.mitty. Tue May 18 17:18 13/480 |
| 155 | & |
| 156 | Message 1: |
| 157 | From root@lucid.mitty.jp Tue May 18 17:18:01 2010 |
| 158 | X-Original-To: root |
| 159 | To: root@lucid.mitty.jp |
| 160 | Date: Tue, 18 May 2010 17:18:01 +0900 (JST) |
| 161 | From: root@lucid.mitty.jp (root) |
| 162 | |
| 163 | -rw-r--r-- 1 root root 0 May 18 17:18 /tmp/3.3863.17:18:01.400271817 |
| 164 | |
| 165 | & |
| 166 | Message 2: |
| 167 | From root@lucid.mitty.jp Tue May 18 17:18:01 2010 |
| 168 | X-Original-To: root |
| 169 | To: root@lucid.mitty.jp |
| 170 | Date: Tue, 18 May 2010 17:18:01 +0900 (JST) |
| 171 | From: root@lucid.mitty.jp (root) |
| 172 | |
| 173 | -rw-r--r-- 1 root root 0 May 18 17:18 /tmp/2.3861.17:18:01.397221662 |
| 174 | |
| 175 | & |
| 176 | Message 3: |
| 177 | From root@lucid.mitty.jp Tue May 18 17:18:01 2010 |
| 178 | X-Original-To: root |
| 179 | To: root@lucid.mitty.jp |
| 180 | Date: Tue, 18 May 2010 17:18:01 +0900 (JST) |
| 181 | From: root@lucid.mitty.jp (root) |
| 182 | |
| 183 | -rw-r--r-- 1 root root 0 May 18 17:18 /tmp/1.3859.17:18:01.401215856 |