Linux implementations use scripts to add crontab scheduling tasks
Linux scripts add crontab scheduling tasks
usual
The usual way to add a crontab scheduling task is to run the command manually:
crontab -e
The task script is then manually added to the file
actual
In practice, we often want to automatically add crontab scheduled tasks when executing program startup scripts, such as periodically deleting logs generated by programs through the crontab task. We can use the following script to automatically add crontab scheduled tasks through the script.
Modify the following script and execute it when the program starts.
#! /bin/sh # Log cleanup script delete_log.sh Replace it with your log cleanup script. Or refer to another blog "Cleaning Logs with Scheduled Tasks in Linux" delete_log_sh_path=/data/delete_log.sh # crontab script executes tasks at 1 am every day cron_command="0 1 * * * /bin/bash ${delete_log_sh_path} >/dev/null 2>&1" # Determining whether a scheduled task exists existing_job=$(crontab -l | grep "${delete_log_sh_path}") if [ -n "${existing_job}" ]; then echo "The cpp log scheduled clearing task already exists. Do not repeat the task!!" exit 1 fi # Add task # Output an existing task to the temporary file crontab -l > cron.txt # append a new task to the end of the file echo "${cron_command}" >> cron.txt # Load the scheduled task crontab txt # Delete a temporary file rm -f cron.txt crontab -l echo "The cpp log scheduled clearing task has been created" exit 0
Linux crontab scheduling task Chinese characters are garbled
Problem phenomenon
The script is executed without garbled characters. After a scheduled task is executed, the script becomes?? .
Problems caused by the original
The crontab task does not obtain system environment variables, causing garbled characters in Chinese
solution
Add the encoding method or add the corresponding environment variable in the execution step
Such as:
1. Add at the beginning of the script:
#! /bin/bash . /etc/profile . ~/.bash_profile export LANG="en_US.UTF-8"
2. /usr/bin/java -Dfile.encoding=UTF-8 -jar ALDTool.jar
Sum up
The above is personal experience, I hope to give you a reference, but also hope that you support the script home.
- This section describes the Linux scheduling task Crontab in detail
- This section describes how to install and use crontabs
- How Do I Use crontab to Add Scheduled tasks in linux
- The Linux Crontab Shell script implements a method for scheduling tasks at the second level
- Troubleshoot the Linux crontab scheduling task execution failure
Related article
-
Description of commands for changing host names in Linux
In this article, Xiaobian gives you a detailed explanation of the knowledge points about Linux to modify the host name command, interested friends can refer to learn.2020-02-02 -
Description Configuring the DNS server in Linux
This article mainly introduces the configuration of DNS server under Linux, has a certain reference value, there is a need to understand.2016-11-11
-
This section describes how to view the kali linux system version
Today, Xiaobian will share a view method of kali linux system version for everyone, which has a good reference value, and I hope to help you. Let's take a look2019-07-07 -
I/O management and common functions of vim in linux system are explained in detail
This article mainly introduces the linux system input and output management and the common functions of vim, very good, has a certain reference value, need friends can refer to the next2019-04-04 -
Description of scheduled tasks in linux
Hello, everyone, this article is mainly about the linux system planning task introduction, interested students come to have a look at it, if it is helpful to you remember to collect it, easy to browse next time2021-12-12 -
Installing Pureftpd in LNMP Indicates how to enable the FTP service and change the FTP port
This article mainly introduces the LNMP installation Pureftpd open FTP service and modify FTP port method, need friends can refer to2016-09-09 -
How does linux Periodically delete files (folders) that are N days old?
This article mainly introduces how linux regularly delete N days ago file (folder) problem, has a good reference value, I hope to help you, if there is a mistake or not considered completely place, hope you are free to advise2023-12-12 -
How to install/configure Tomcat in Linux environment
This article mainly introduces the Linux environment to build the installation/configuration of Tomcat method, has a certain reference value, interested partners can refer to it2017-06-06 -
Ubuntu linux install PHP5.3.0 command
The official version of 5.3.0 has been released for some time, see the script home above there is a tutorial to install php5.3 under windows, so think of writing a PHP5.3 installation tutorial on linux.2009-09-09
Latest comments