In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use Linux's anacron command". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Linux's anacron command".
Prepare for automation
The word "automation" is both daunting and fascinating. I find it helpful to deal with it in a modular way.
1. What do you want to achieve?
First of all, know what you want to produce. Are you going to watermark the picture? Delete files from a cluttered directory? Perform backup of important data? Define tasks for yourself so that you know what your goals are. If there is a task that you find yourself doing every day, or even more than once a day, it may be a candidate for automation.
2. Learn the applications you need
Break down large tasks into small components and learn how to produce each result manually but in a repeatable and predictable manner. Many of the things you can do on Linux can be done with scripts, but it's important to recognize your current limitations. Learning how to automatically resize a few pictures so that they can be easily emailed is a far cry from using machine learning to generate elaborate works of art for your weekly newsletter. There are some things you can learn in one afternoon, while another can take years. However, we all have to start somewhere, so just start from an early age and always pay attention to ways to improve.
3. Automation
Use an automated tool on Linux to implement it on a regular basis. This is the step introduced in this article!
To automate something, you need a script to automate a task. When testing, it's best to keep it simple, so the automated task of this article is to create a file called hello in the / tmp directory.
#! / bin/sh touch / tmp/hello
Copy and paste this simple script into a text file and name it example.
Cron
The built-in automation solution that every installed Linux system has is the cron system. Linux users tend to refer to cron generally as the method you use to schedule tasks (often referred to as "cron jobs"), but there are multiple applications that provide the functionality of cron. The most common is cronie;, which has the advantage that it doesn't assume that your computer is always on, like the cron applications designed for system administrators in history.
Verify which cron system your Linux distribution provides. If it is not cronie, you can install cronie from the software repository of the distribution. If your distribution doesn't have a cronie package, you can use the old anacron package instead. The anacron command is included in the cronie, so no matter how you get it, make sure you have the anacron command on your system before continuing. Anacron may require administrator root privileges, depending on your settings.
$which anacron/usr/sbin/anacron
Anacron's job is to make sure that your automation assignments are performed on a regular basis. To do this, anacron checks to find out when the job was last run, and then checks how often you tell it to run the job.
Suppose you set anacron to run the script every five days. Every time you turn on your computer or wake it up from sleep, anacron scans its log to determine if you need to run a job. If a job ran five days or more ago, anacron runs the job.
Cron job
Many Linux systems are bundled with maintenance work for cron to perform. I like to separate my work from the work of the system, so I created a directory in my home directory. Specifically, there is a hidden folder called ~ /. Local ("local" means it is customized for your user account, not for your "global" computer system), so I created a subdirectory etc/cron.daily to serve as cron's home directory on my system. You must also create a spool directory to track when the job was last run.
$mkdir-p ~ / .local/etc/cron.daily ~ / .var/spool/anacron
You can put any script you want to run regularly into the ~ / .local/etc/cron.daily directory. Now copy the example script to the directory and use the chmod command to make it executable.
$cp example ~ / .local / etc/cron.daily# chmod + x ~ / .local/etc/cron.daily/example
Next, set up anacron to run any scripts located in the ~ / .local/etc/cron.daily directory.
Anacron
By default, most of the cron system is considered the domain of system administrators because it is often used for important low-level tasks, such as rotating log files and updating certificates. The configuration demonstrated in this article is designed for ordinary users to set up personal automation tasks.
To configure anacron to run your cron job, create a configuration file at / .local/etc/anacrontab:
SHELL=/bin/shPATH=/sbin:/bin:/usr/sbin:/usr/bin1 0 cron.mine run-parts / home/tux/.local/etc/cron.daily/
This file tells anacron that every new day (that is, every day), after a 0-minute delay, run-parts all executable scripts found in ~ /. Local/etc/cron.daily. Sometimes, a delay of a few minutes is used so that your computer will not be hit by all possible tasks after you log in. However, this setting is suitable for testing.
The cron.mine value is an arbitrary name for the process. I call it cron.mine, but you can also call it cron.personal or penguin or whatever you want.
Verify the syntax of your anacrontab file:
$anacron-T-t ~ / .local/etc/anacrontab\-S / home/tux/.var/spool/anacron
Silence means success.
Add anacron to .profile
Finally, you must make sure that anacron runs in your local configuration. Because you are running anacron as a normal user rather than a root user, you must boot it to your local configuration: the anacrontab file that tells anacron what to do, and the spool directory that helps anacron track how many days each job was last executed:
Anacron-fn-t / home/tux/.local/etc/anacrontab\-S / home/tux/.var/spool/anacron
The-fn option tells anacron to ignore the timestamp, which means that you force it to run your cron job anyway. This is entirely for testing purposes.
Test your cron homework
Now that everything is set up, you can test the homework. Technically, you can test without rebooting, but rebooting makes the most sense because it is designed to handle interrupted and irregular login sessions. Take the time to restart your computer, log in, and look for test files:
$ls / tmp/hello/tmp/hello
Assuming that the file exists, your sample script has been successfully executed. Now you can remove the test option from ~ / .profile and leave this as your final configuration.
Anacron-t / home/tux/.local/etc/anacrontab\-S / home/tux/.var/spool/anacron uses anacron
You have configured your personal automation infrastructure, so you can put any script you want your computer to manage for you in the ~ / .local/etc/cron.daily directory, and it will run as planned.
It depends on how often you want the job to run. The sample script is executed once a day. Obviously, it depends on whether your computer is turned on and awake on any given day. If you use the computer on Friday, but set it on the weekend, the script will not run on Saturday and Sunday. On Monday, however, the script will be executed, because anacron will know that at least one day has passed. You can add weekly, fortnightly, or even monthly directories to ~ / .local/etc to arrange various intervals.
To add a new interval:
Add a directory to ~ / .local/etc (for example, cron.weekly).
Add a line to ~ / .local/etc/anacrontab to run the script in the new directory. For weekly intervals, the configuration is as follows. 7 0 cron.mine run-parts / home/tux/.local/etc/cron.weekly/ (a value of 0 can be selected for some minutes to delay the start of the script appropriately).
Put your script in the cron.weekly directory.
Thank you for your reading, the above is the content of "how to use Linux's anacron command". After the study of this article, I believe you have a deeper understanding of how to use Linux's anacron command, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.