search
HomeBackend DevelopmentPHP TutorialLinux uses crontab to implement PHP execution plan scheduled tasks_PHP tutorial

First, let’s talk about cron, which is a scheduled execution tool under Linux. Users other than root can use the crontab tool to configure cron tasks. All user-defined crontabs are saved in the /var/spool/cron directory and executed using the identity of the user who created them. To create a crontab entry as a user, log in as that user, and then type the crontab -e command to edit the user's crontab. This file uses the same format as /etc/crontab. When the changes to the crontab are saved, the crontab file is saved according to the username and written to the file /var/spool/cron/username. The cron daemon checks the /etc/crontab file, etc/cron.d/ directory, and /var/spool/cron directory for changes every minute. If changes are found, they are loaded into memory. This way, you don't have to restart the daemon when a crontab file changes.

Install crontab:

yum install crontabs

Instructions:
/sbin/service crond start //Start the service
/sbin/service crond stop //Close the service
/sbin/service crond restart //Restart the service
/sbin/ service crond reload //Reload configuration

View crontab service status: service crond status

Manually start the crontab service: service crond start

Check whether the crontab service has been set to start at boot, execute the command: ntsysv

Add automatic startup at boot:
chkconfig –level 35 crond on

crontab command:

Function description: Set timer.

Syntax: crontab [-u ][configuration file] or crontab [-u ][-elr]

Additional explanation: cron is a resident service that provides a timer function, allowing users to execute preset instructions or programs at a specific time. As long as the user can edit the timer configuration file, the timer function can be used. The configuration file format is as follows:
Minute Hour Day Month DayOFWeek Command

Parameters:
-e Edit the timer settings for this user.
-l List the timer settings for this user.
-r Delete the timer settings for this user.
-u Specifies the user name to set the timer.

crontab format:

Basic format:

Minutes Hours Days Months Weeks Commands

*                                                    

The first column represents minutes 1 to 59. Each minute is represented by * or */1

The second column represents hours 1 to 23 (0 represents 0 o'clock)
The third column represents dates 1 to 31
The 4th column represents the month 1~12
The 5th column identifies the week 0~6 (0 means Sunday)
The 6th column represents the command to be run

Remember the meanings of several special symbols:

“*” represents a number within the value range,
“/” represents “every”,
“-” represents from a certain number to a certain numbers,
"," separates several discrete numbers

# Use the hash sign to prefix a comment

# +————- minute (0 – 59)
# | +————- hour (0 – 23)
# | | +——- day of month (1 – 31)
# | | | +——- month (1 – 12)
# | | | | +——- day of week (0 – 7 ) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed

A few examples of crontab are as follows:

(1) First example.

30 21 * * * /etc/init.d/nginx restart

Restart nginx at 21:30 every night.

(2) The second example, which is the example tested in this tutorial

* * * * * /usr/bin/php -f /root/test.php >> test.log

Execute the /root/test.php file every minute and output the results to test.log.

After completing the above basic work, let’s take a look at how to use crontab to execute PHP scripts regularly:

(1) I created a new test.php file under /root with the following content:

Copy code The code is as follows:
            #!/usr/bin/php -q
echo date('Y-m-d H:i:s')."from http://www.phpddt.com n";
?>

Note: You can use whereis php to find the location of the php execution file.

(2) Then crontab -e writes the following shell:

Copy code The code is as follows:
* * * * * /usr/bin/php -f /root/test. php>> test.log

Note: test.php must be an executable file: chmod +x test.php

The test results are normal, the screenshot is as follows:

Linux uses crontab to implement PHP execution plan scheduled tasks_PHP tutorial

Of course you can use crontab -e to continue adding tasks. You can see a root file under /var/spool/cron.
Use Windows to schedule tasks directly under Windows, and just open the web page through bat. It is not copied like Linux.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/768125.htmlTechArticleFirst let’s talk about cron, which is a scheduled execution tool under Linux. Users other than root can use the crontab tool to configure cron tasks. All user-defined crontabs are saved in...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What data can be stored in a PHP session?What data can be stored in a PHP session?May 02, 2025 am 12:17 AM

PHPsessionscanstorestrings,numbers,arrays,andobjects.1.Strings:textdatalikeusernames.2.Numbers:integersorfloatsforcounters.3.Arrays:listslikeshoppingcarts.4.Objects:complexstructuresthatareserialized.

How do you start a PHP session?How do you start a PHP session?May 02, 2025 am 12:16 AM

TostartaPHPsession,usesession_start()atthescript'sbeginning.1)Placeitbeforeanyoutputtosetthesessioncookie.2)Usesessionsforuserdatalikeloginstatusorshoppingcarts.3)RegeneratesessionIDstopreventfixationattacks.4)Considerusingadatabaseforsessionstoragei

What is session regeneration, and how does it improve security?What is session regeneration, and how does it improve security?May 02, 2025 am 12:15 AM

Session regeneration refers to generating a new session ID and invalidating the old ID when the user performs sensitive operations in case of session fixed attacks. The implementation steps include: 1. Detect sensitive operations, 2. Generate new session ID, 3. Destroy old session ID, 4. Update user-side session information.

What are some performance considerations when using PHP sessions?What are some performance considerations when using PHP sessions?May 02, 2025 am 12:11 AM

PHP sessions have a significant impact on application performance. Optimization methods include: 1. Use a database to store session data to improve response speed; 2. Reduce the use of session data and only store necessary information; 3. Use a non-blocking session processor to improve concurrency capabilities; 4. Adjust the session expiration time to balance user experience and server burden; 5. Use persistent sessions to reduce the number of data read and write times.

How do PHP sessions differ from cookies?How do PHP sessions differ from cookies?May 02, 2025 am 12:03 AM

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

How does PHP identify a user's session?How does PHP identify a user's session?May 01, 2025 am 12:23 AM

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

What are some best practices for securing PHP sessions?What are some best practices for securing PHP sessions?May 01, 2025 am 12:22 AM

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

Where are PHP session files stored by default?Where are PHP session files stored by default?May 01, 2025 am 12:15 AM

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.