wiki:TipAndDoc/cron

Version 8 (modified by mitty, 15 years ago) (diff)

--

遅延実行

遅延実行テスト

  • 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'