Talk about running PHP scheduled tasks

Source: Internet
Author: User
Tags lstat

The planning tasks used by the company are all about this:

*/2 * * * * * root cd/opt/xxxx/test_s1/html/xxxx/admin; PHP index.php task Testone >/dev/null2>&1*/2 * * * * Root cd/opt/xxxx/test _s1/html/xxxx/admin; PHP index.php task testtwo >/dev/null2>&1

You can see that the output is redirected to an empty device with a standard error, for some reason. A look at some of the information, described here:

1.SSH Login Server
2. Create a new PHP file test.php with the following code:

<? phpsleep ("aaa\n"; file_put_contents (" /tmp/test.txt " , Time ());? >

3. Execute the test.php program with the following command

$ PHP test.php &

View the contents of the/tmp/test.txt file as 1497613738

4. Then execute the following command again. Immediately after the command is executed, exit the login using the Exit command

$ PHP test.php &

5 then SSH to the server, found that the contents of the/tmp/test.txt file is still 1497613738. Indicates that the File_put_contents function did not execute or did not succeed when Test.php was executed the second time.

6 use STRACE for tracking:

Under normal conditions of tracking:
1475
Results:
Process1475Attachedrestart_syscall (&lt resuming interrupted call ...>) =0Write (1,"aaa\n", 4AAA)=4Lstat ("/tmp/test.txt",0x7ffe792ebe90)  = -1ENOENT (No such file or directory) Lstat ("/ tmp", {st_mode=s_ifdir| S_isvtx|0777, st_size=65536, ...}) =0Open ("/tmp/test.txt", o_wronly| O_creat| O_trunc,0666) =3Fstat (3, {st_mode=s_ifreg|0644, st_size=0, ...}) =0Lseek (3,0, seek_cur) =0Write (3,"1497614186",Ten)              =TenClose (3)                                =0Munmap (0x7f4840239000,528384)          =0Close (2)                                =0Close (1)                                =0Munmap (0x7f4847510000,4096)            =0Close (0)                                =0Munmap (0x7f4847511000,4096)            =0sched_getaffinity (1475, -, {FFFFFF,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}) = -sched_setaffinity (0, -, {FFFFFF,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}) =0Munmap (0x7f48404c8000,2119936)         =0Munmap (0x7f48406ce000,2345056)         =0Munmap (0x7f4840b39000,2162536)         =0Munmap (0x7f484090b000,2282472)         =0Munmap (0x7f4840d49000,323584)          =0Exit_group (0) = + + + exited with0+++[1]+ Done PHP test.php

Follow-up after exiting:

Strace-p 3881

Restart_syscall (< .... resuming interrupted call ...>) =0Write (1,"aaa\n",4)                    = -1EIO (input/output error) Munmap (0x7f6b7fc04000,528384)          =0Close (2)                                =0Close (1)                                =0Munmap (0x7f6b86edb000,4096)            =0Close (0)                                =0Munmap (0x7f6b86edc000,4096)            =0sched_getaffinity (3881, -, {FFFFFF,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}) = -sched_setaffinity (0, -, {FFFFFF,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}) =0Munmap (0x7f6b7fe93000,2119936)         =0Munmap (0x7f6b80099000,2345056)         =0Munmap (0x7f6b80504000,2162536)         =0Munmap (0x7f6b802d6000,2282472)         =0Munmap (0x7f6b80714000,323584)          =0Exit_group (0) = + + + exited with0+++

By contrast, I see an input/Output error in Echo, and no file_put_contents function was executed at the end.

7 reasons:

Why quit landing, and then landing, there will be a Eio error? This is about session processing with Linux.
When a user ssh into a server, a session is also started. After the session begins, standard input (stdin), standard output (STDOUT), standard error (STDERR) are connected to a corresponding terminal (pty).
After the user logs in, any standard output will be reflected in the terminal. The file handle for standard output is 1. Therefore, echo ("aaa\n") in PHP causes the system to call write (1, "aaa\n", 4). The aaa\n is written in the terminal.
? When the user exits the login, a session ends. At the end of the session, modify all file handles that open the terminal and change it to unreadable and not writable;
When the user exits the login and executes write (1, "aaa\n", 4), it will report Eio error. Because the terminal handle is not writable. Eio error occurs, which causes the process to end.

Solutions
Method One:
Use the redirect symbol & redirect The standard output to the hole.

$ php test.php >/dev/null2 >&1 &   here & can be used or not

Method Two:
Use Nohup.

$ nohup php test.php &

Description

If you are running a process and you feel that the process will not end when you exit the account, you can use the Nohup command.

This command can continue to run the process after you exit the account/close the terminal. Nohup is the meaning of not hanging (n ohang up).

And our company is using the first method, about/dev/null 2 >&1 & Description, see another article http://www.cnblogs.com/ Zhenghongxin/p/7029173.html

Talk about running PHP scheduled tasks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.