談一談PHP計畫任務

2021-09-10 12:07:41 字數 4083 閱讀 6725

公司所用計畫任務均是大概這樣子的:

*/2 * * * * root cd /opt/***x/test_s1/html/***x/admin; php index.php task testone >/dev/null

2>&1

*/2 * * * * root cd /opt/***x/test_s1/html/***x/admin; php index.php task testtwo >/dev/null

2>&1

可以看到把輸出與標準錯誤進行重定向到空裝置了,這樣做是有一定原因的。查閱了一些資料,在這裡描述一下:

1.ssh登陸伺服器

2.新建乙個php檔案test.php,**如下:

<?php

sleep(

50);

echo

"aaa\n";

file_put_contents(

"/tmp/test.txt

",time());

?>

3.用以下命令執行test.php程式

$ php test.php &

檢視 /tmp/test.txt 檔案的內容為1497613738

4.然後再次執行如下命令。命令執行後,馬上使用exit命令退出登陸

$ php test.php &

5 然後ssh登陸伺服器,發現/tmp/test.txt 檔案的內容依然是 1497613738。說明第二次執行test.php時,file_put_contents函式沒有執行,或者沒有執行成功。

6 使用strace進行追蹤:

正常情況下的追蹤:

strace -p 1475

結果:

process 1475

attached

restart_syscall(

<... resuming interrupted call ...>) = 0

write(

1, "

aaa\n

", 4aaa

) = 4

lstat(

"/tmp/test.txt

", 0x7ffe792ebe90) = -1

enoent (no such file or directory)

lstat(

"/tmp

", ) = 0

open(

"/tmp/test.txt

", o_wronly|o_creat|o_trunc, 0666) = 3

fstat(

3, ) = 0

lseek(

3, 0, seek_cur) = 0

write(

3, "

1497614186

", 10) = 10

close(

3) = 0

munmap(

0x7f4840239000, 528384) = 0

close(

2) = 0

close(

1) = 0

munmap(

0x7f4847510000, 4096) = 0

close(

0) = 0

munmap(

0x7f4847511000, 4096) = 0

sched_getaffinity(

1475, 128, ) = 128

sched_setaffinity(

0, 128, ) = 0

munmap(

0x7f48404c8000, 2119936) = 0

munmap(

0x7f48406ce000, 2345056) = 0

munmap(

0x7f4840b39000, 2162536) = 0

munmap(

0x7f484090b000, 2282472) = 0

munmap(

0x7f4840d49000, 323584) = 0

exit_group(

0) = ?

+++ exited with 0 +++[

1]+ done php test.php

退出後再登陸的追蹤:

strace -p 3881

restart_syscall(<... resuming interrupted call ...>) = 0

write(

1, "

aaa\n

", 4) = -1 eio (input/output error)

munmap(

0x7f6b7fc04000, 528384) = 0

close(

2) = 0

close(

1) = 0

munmap(

0x7f6b86edb000, 4096) = 0

close(

0) = 0

munmap(

0x7f6b86edc000, 4096) = 0

sched_getaffinity(

3881, 128, ) = 128

sched_setaffinity(

0, 128, ) = 0

munmap(

0x7f6b7fe93000, 2119936) = 0

munmap(

0x7f6b80099000, 2345056) = 0

munmap(

0x7f6b80504000, 2162536) = 0

munmap(

0x7f6b802d6000, 2282472) = 0

munmap(

0x7f6b80714000, 323584) = 0

exit_group(

0) = ?

+++ exited with 0 +++

通過對比可以看到echo處出現了 輸入/輸出錯誤,最終沒有執行file_put_contents函式。

7 原因:

解決辦法

方法一:

​使用重定向符號&把標準輸出重定向到空洞。

$ php test.php > /dev/null

2 >&1 & 這裡的& 可以用也可以不用

方法二:

使用nohup。

$ nohup php test.php &

說明:

如果你正在執行乙個程序,而且你覺得在退出帳戶時該程序還不會結束,那麼可以使用nohup命令。

該命令可以在你退出帳戶/關閉終端之後繼續執行相應的程序。nohup就是不掛起的意思( n ohang up)。

而我們公司採用的是第一種方法,關於對/dev/null 2 >&1 &

的描述,看另外一篇文章

posted @

2017-06-16 20:12

hongxinerke 閱讀(

...)

編輯收藏

PHP計畫任務

server 2008 d soft php package php php cgi.exe f d wwwroot tlbuyuncom wwwroot up data.php php路徑 f 執行檔案路徑 liunx 沒測試過 linux 的指令碼實現 這裡主要使用到crontab這個命令,使用...

小談 Centos 計畫任務

檢視系統的計畫任務 cat etc crontab eg.root localhost cat etc crontab shell bin bash path sbin bin usr sbin usr bin mailto root for details see man 4 crontabs e...

PHP執行計畫任務

在linux的終端下實現守護程序,可以使用crontab來計畫php指令碼執行。crontab命令的功能是在一定的時間間隔排程一些命令的執行。crontab 使用方法 crontab e l r 檔名 e 編輯任務 l 顯示任務資訊 r 刪除定時執行任務資訊 crontab的格式 command 分...