I. Scheduled Tasks#
Main content: Periodic scheduled tasks crontab
==/var/spool/cron/ directory for storing scheduled tasks==
==/var/log/cron log file==
==/etc/cron cron.daily/ cron.hourly/ cron.monthly/ cron.weekly/ crontab's ride==
/etc/crontab
==crontab -l==
==crontab -e==
crontab ==-u cali== -e
crontab -u cali -l
==How to know if the scheduled task is executed?==
==If the user is not logged in, will the scheduled task execute? Answer: Yes==
==Hackers may also use scheduled tasks to execute programs at intervals, how do you check?==
==Anacron will detect any scheduled tasks that were not executed due to the computer being shut down because of a system failure, and will re-execute them after normal startup==
at one-time scheduled tasks
==at 11:45==
==at -l==
cron#
Scheduled tasks
1. What is a scheduled task? (Alarm clock)
Plan when to do something
One-time scheduled tasks
at
Periodic scheduled tasks
crontab
The process for executing scheduled tasks in the Linux system --》==crond==
crond checks all users' scheduled tasks ==every minute==
# ps aux|grep crond
root 887 0.0 0.1 36300 3508 ? Ss Sep 24 0:00 /usr/sbin/==crond== -n
root 16948 0.0 0.0 12320 984 pts/0 S+ 10:05 0:00 grep --color=auto ==crond==
# cd ==/var/spool/cron/ (Note: Directory for storing scheduled tasks)==
# ls
#
Scheduled tasks Principle
Principle Format and syntax of time (written test)
(Note: Commands that each user can use)
# ==crontab -l (Note: View current user's scheduled tasks)== (Each user can only see their own, root can see all)
no crontab for root
#
# ==crontab -e (Note: Create scheduled tasks)==
…… (Note: vim editor is essentially a text file)
# crontab -l
==30 4 * * *== bash /lianxi/9_25/backup_log.sh
==*/5 * * * *== bash /lianxi/9_25/backup_log.sh
==50 3 6-9 10 *== bash /lianxi/9_25/backup_log.sh
==*== Any time within this range
==,== Multiple non-continuous time points
==-== A continuous time range
==/n== Specify the frequency of time intervals
==(Note: The minimum time interval for the crond process is 1 minute)==
(Note: Time cannot conflict; dates and days of the week cannot conflict)
#
# cd ==/var/spool/cron/ (Note: Directory for storing scheduled tasks, one user corresponds to one filename)==
# ls
cali liangluyao ==root==
# ==cat root (Note: The file contains what was edited with crontab -e)==
30 4 * * * bash /lianxi/9_25/backup_log.sh
*/5 * * * * bash /lianxi/9_25/backup_log.sh
50 3 6-9 10 * bash /lianxi/9_25/backup_log.sh
#
# ll ==(Note: Each user's scheduled tasks will be placed in the /var/spool/cron/ directory)==
Total 12
-rw-------. 1 ==cali== cali 31 Sep 25 10:27 cali
-rw-------. 1 ==liangluyao== liangluyao 31 Sep 25 10:28 liangluyao
-rw-------. 1 ==root== root 139 Sep 25 10:25 root
#
==Question: How to know if the scheduled task is executed?==
==Answer: Check the log file, /var/log/cron.==
==Or directly check the effect.==
======================
Log file== (Note: Records the scheduled tasks executed by crond, stored in /var/log/cron, used for troubleshooting.)==
# tail -f ==/var/log/cron== (Note: tail -f watches the end of the file)
Sep 25 10:30:01 sanchuang-linux CROND[17143]: (root) CMD (bash /lianxi/9_25/backup_log.sh)
Sep 25 10:30:01 sanchuang-linux CROND[17147]: (liangluyao) CMD (date >>~/liang.txt)
Sep 25 10:30:02 sanchuang-linux CROND[17119]: (root) CMDOUT (tar: Removing leading “/” from member names)
Sep 25 10:30:02 sanchuang-linux CROND[17119]: (root) CMDOUT (tar: /var/log/audit/audit.log: File changed as we read it)
Sep 25 10:30:02 sanchuang-linux CROND[17155]: (cali) CMD (date >>~/cali.txt)
Example 1 (root user):
Automatically start the sshd service at 7:50 AM every day and stop it at 10:50 PM
==service sshd start (Note: Start sshd service)==
==service sshd stop (Note: Stop sshd service)==
50 7 * * * service sshd start
50 22 * * * service sshd stop
Every 5 days at 12:00 PM ==clear== the FTP server's public directory /var/ftp/pub
0 12 /5 * * rm -rf /var/ftp/pub==/ (Note: Clear: delete everything inside)==
Restart the httpd service at 7:30 AM every Saturday
==service httpd restart (Note: Restart httpd service)==
30 7 * * 6 service httpd restart
On Mondays, Wednesdays, and Fridays at 5:30 PM, back up the /etc/httpd directory
30 17 * * 1,3,5 tar czf /==backup==/httpd.tar.gz /etc/httpd ==(Note: /backup/ is the backup directory)==
Example 2 (jerry user): ==(Note: Home directory: ~/)==
At 11:55 PM every Sunday, copy the contents of the “/etc/passwd” file to the ==home directory==, saved as pwd.txt file
55 23 * * 0 ==/usr/bin/cp== /etc/passwd ~/pwd.txt ==(Note: Absolute path of the command)==
/etc/crontab is a configuration file for cron
# vim /etc/crontab
SHELL=/bin/bash (Note: Use /bin/bash to execute commands in scheduled tasks)
==PATH===/sbin:/bin:/usr/sbin:/usr/bin ==(Note: It has its own PATH variable, different from the shell's PATH variable)==
MAILTO=root ==(Note: It is recommended to use the absolute path of commands in all scheduled tasks due to PATH variable issues)==
==(Note↑: By default, if there is a problem with the scheduled task, crontab will send an email to root)==
# For details see man 4 crontabs# Example of job definition: (Note: Explanation of commands)
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
# crontab ==-u cali== -e ==(Note: Create scheduled tasks for cali using the root user)==
# crontab -u cali -l (Note: View)
- If the computer is shut down, will the scheduled task execute? Answer: No
==2. If the user is not logged in, will the scheduled task execute? Answer: Yes==
# ==w (Note: Check which users are logged in from where)==
# ==tail -f /var/log/cron (Note: Log file)==
==3. Hackers may also use scheduled tasks to execute programs at intervals, how do you check?==
Check each scheduled task file in the ==/var/spool/cron== directory
Check the log ==/var/log/cron==
Crontd's ride directory (as follows)
#
# ==cd /etc/cron*******==
cron.d/ ==cron.daily/== cron.deny ==cron.hourly/ cron.monthly/== crontab ==cron.weekly/==
# cd /etc/cron
(Note: Crontab's ride)
==Hackers can also place programs to be executed in these folders==
==cron.daily --》Tasks that crond is required to execute daily in the Linux system==
==cron.hourly --》Tasks that crond is required to execute hourly in the Linux system==
==cron.monthly --》Tasks that crond is required to execute monthly in the Linux system==
==cron.weekly --》Tasks that crond is required to execute weekly in the Linux system==
(Note: # ls cron.daily
logrotate (Executed daily, crond runs once a day (script executed with sh))
# ls cron.hourly
0==anacron (Anacron detects any scheduled tasks that were not executed due to the computer being shut down because of a system failure, and will re-execute them after normal startup)==
at#
The at command for one-time scheduled tasks
yum install at -y
# ==at 11:45==
warning: commands will be executed using /bin/sh
at> touch $(date +%F).txt
at> ==(Note: Exit Ctrl+D)==
job 1 at Fri Sep 25 11:45:00 2020
Can't open /var/run/atd.pid to signal atd. No atd running?
# ==at -l (Note: View)==
1 Fri Sep 25 11:45:00 2020 a root
#
II. Scheduled Task Practice - Backup Script#
Main content: ==chmod 777== /opt/fnum.txt (Note: ==Set any user to read, write, and execute the file /opt/fnum.txt==)
==2>/dev/null==
==/dev/null is a special file, equivalent to a black hole file, any content redirected to this file will disappear and not be saved==
==Correct output redirection==
==>==
==>>==
==Error output redirection==
==2>==
==2>> Append==
==Redirect both correct and error outputs to one file==
==&>==
==&>> Append==
==>1.txt Clear the contents of 1.txt file==
==Delete all scheduled tasks of user xull== rm -rf /var/spool/cron/xull
==# crontab -u xull -r (Note *)==
Cancel scheduled tasks
Script + Scheduled Tasks = Automation
First write the script, then create the scheduled task
Scheduled Task Experiment#
Scheduled task experiment:
- Write a scheduled task for the root user: Every Tuesday, Thursday, and Saturday from 10 AM to 5 PM, every 5 minutes automatically back up the two important system files /etc/passwd and /etc/shadow to a file named user.backup date and time.tar.gz (for example: user.201202011615.tar.gz) and place the backup file in the /opt/ directory.
backup_shadow_pwd.sh
# cat backup_shadow_pwd.sh
#!/bin/bash
mkdir -p /opt #Note: date +%Y%m%d%H%M%S , ==There is a space after date==
tar czf /opt/user.$(date +%Y%m%d%H%M%S).tar.gz /etc/passwd /etc/shadow
# $(date +%Y-%m-%d-%H) Note the space above
==crontab -e==
*/5 10-17 * * 2,4,6 bash /lianxi/9_25/backup_shadow_pwd.sh #==(Note: Path must be correct, absolute path)==
- ==The root user== created a file fnum.txt in /opt== specifically to count the number of files owned by ordinary users. Please ==write a scheduled task for the ordinary user hello==: Every 5 minutes, automatically save the number of files belonging to the hello user in the /opt/fnum.txt file. The content format is: “Date Time Name Number of files owned: Count”. Each line is separated by “***************”
file_num.sh
# touch /opt/fnum.txt (Note: ==Create an empty file, ordinary users cannot create it, root user creates it==)
# ==chmod 777== /opt/fnum.txt (Note: ==Set any user to read, write, and execute the file /opt/fnum.txt==)
[cali@sanchuang-linux ~]$ crontab -l
*/5 * * * * date >>~/cali.txt
5 12-18 * * * date >>~/cali.txt
*/1 * * * * bash /home/cali/file_num.sh
[cali@sanchuang-linux ~]$ pwd
/home/cali
$ cat file_num.sh ==(Note: The script is placed in the home directory of the ordinary user)==
#!/bin/bash
fnum=$(find / -user cali -type f ==2>/dev/null== |wc -l) ==(Note: Ordinary user file permissions are insufficient, causing errors)==
(Note↑: Error redirection)
ctime=$(date +%F_%H%M%S)
echo "${ctime} cali the number of files : $fnum" ==>>/opt/fnum.txt (Note: Append redirection to write to the file)==
echo "******************************" ==>>/opt/fnum.txt (Note: Append redirection to write to the file)==
$
$ crontab -e ==(Note: The user creates the scheduled task for themselves, ==cali creates)==
*/1 * * * * bash /home/cali/file_num.sh
$ pwd
/home/cali ==(Note: The path of the script)==
Each line is separated by “***************” Solution
# cat a.sh
echo "helllo"
echo "***************"
echo "cali"
echo "***************"
#
# num=$(find / -user liangluyao |wc -l)
$ find / -user cali -type f 2>/dev/null|wc -l
==/dev/null is a special file, equivalent to a black hole file, any content redirected to this file will disappear and not be saved==
null zero, empty, invalid
Redirection#
==Correct output redirection==
==>==
==>>==
======
==Error output redirection==
==2>==
==2>> Append==
======
==Redirect both correct and error outputs to one file==
==&>==
==&>> Append==
==ls command successfully redirects to 1.txt, fails to redirect to 2.txt==
$ ==ls >1.txt 2>2.txt==
$ ==>1.txt Clear the contents of 1.txt file==
$ cat 1.txt
$
$ ls &>1.txt ==Redirect both correct and error outputs to one file==
Practice Summary#
Practice summary:
Questions:
2>/dev/null
How to check the effect
# cat fnum.txt
# tail -f /var/log/cron (Log file)
- Cancel scheduled tasks
==Delete all scheduled tasks of user xull== rm -rf /var/spool/cron/xull
==# crontab -u xull -r (Note *)==
Comment out or delete scheduled tasks in the file #*/1 * * * * bash /home/xull/file_num.sh
# cd /var/spool/cron/ ——》# rm -rf xull ==# crontab -u xull -r==
# cd /var/spool/cron/ ——》# vim xull ——》 Add # to comment out
How to create scheduled tasks for a specific user
File path issues and permission issues (root can access the files in the task location, but ordinary users cannot, they can only access limited paths)
Append all scheduled tasks of the root user to the scheduled tasks of the xull user
# cd /var/spool/cron
# cat root >>xull (Note: Pay attention to permission issues, whether access is allowed)
Each user's scheduled tasks are stored in a file /var/spool/cron/
The root user created a file fnum.txt in /opt2 specifically to count the number of files owned by ordinary users. Please write a scheduled task for the ordinary user xull: Every 5 minutes, automatically save the ==number of files belonging to the xull user in the system== to the /opt2/fnum.txt file. The content format is: “Date Time Name Number of files owned: Count”. Each line is separated by “***************”
Use the root user to create the /opt2 directory
mkdir /opt2
cd /opt2/
touch /opt2/fnum.txt file
chmod 777 /opt2/fnum.txt Authorization
==The xull user needs to write the script and create the scheduled task (ordinary user)==
==Script: It is recommended to place it in the user's home directory==
# su - xull
Last login: Fri Sep 25 15:53:35 CST 2020 pts/1
$ pwd
==/home/xull==
$ ls
$ vim file_num.sh (Note: The script is placed in the home directory of the user xull)
$ cat file_num.sh
#!/bin/bash
num=$(find / -user xull -type f 2>/dev/null |wc -l)
#current time (Note: Current time)
ctime=$(date +%F%H%M%S)
echo "${ctime} xull Number of files is $num" >>/opt2/fnum.txt (Note: >> append)
echo "***************" >>/opt2/fnum.txt (Note: Separate lines)
$
(Note: vim is interactive. The script runs in the background, using echo)
$ crontab -e
*/1 * * * * bash /home/xull/file_num.sh
$
Switch to the root user
$ exit
# cd /var/spool/cron/ (Note: This directory stores scheduled tasks)
# ls
cali liangluyao root ==xull==
#cat xull (Note: The file stores scheduled tasks)
==*/1 * * * * bash /home/xull/file_num.sh==
III. Hacker - Scheduled Tasks#
Main content: Suppose you are a hacker and want to restart someone else's server every 10 minutes or once a day
Shutdown command: init 6
reboot
==1. Stop the crond service==
# service crond stop
- Find directories that may store scheduled tasks
/var/spool/cron (Note: Move it away first)
/etc/cron.d (Note: Related to cron.hourly)
/etc/cron.weekly
/etc/cron.daily
/etc/cron.monthly
- Check and if no problems are found, restart the crond service
# service crond start
Redirecting to /bin/systemctl start crond.service
# ps aux|grep crond
root 1506 3.6 0.1 36304 3656 ? Ss 16:54 0:00 /usr/sbin/crond -n
root 1511 0.0 0.0 12320 1052 pts/0 S+ 16:54 0:00 grep --color=auto crond
#
If there are indeed any
# tail -f /var/log/cron (Log file)
(Note: Troubleshoot through the log)
=============================
The central location for scheduled tasks is /var/spool/cron/
# cd /var/spool/cron/
# ls
cali liangluyao root xull
# cat cali
……(Check for any suspicious entries, vim comment them all out)
Or move all files away, as follows
# ls
cali liangluyao root xull
# mkdir /backup (Note: Create a /backup directory under root) /backup backup directory
mv * /backup/ (Move files cali liangluyao root xull to /backup directory)
Scheduled tasks stopped, crond will not execute
# cd /backup/
# ls
cali liangluyao root xull
# cat cali (Note: After moving the scheduled task files away, check each one in the /backup directory)
……
# cat liangluyao
……
# w (Note: Check which users are logged in)
=======================================
Ride
# cd /etc/cron.daily
# ls
# vim …
# cd /etc/cron.hourly/
# cd /etc/cron.monthly
# cd /etc/cron.weekly
# cd /etc/cron.d (Note: Executes at the first minute of every hour)
(Note: run-parts is a command that runs all files in this folder)
=======================================================
Boot startup process
# cd /root (Note: In the root user's home directory)
# ls -a (Note: Hidden files)
# vim .bashrc
# vim .bash_profile
# vim /etc/profile (Note: Can quickly filter search for /reboot /init)
# vim /etc/bashrc (Note: Can quickly filter search for /reboot /init)
# cat /etc/rc.local
Suppose you are a hacker and want to restart someone else's server every 10 minutes or once a day
====
192.168.0.45 root 123456
Shutdown command: init 6
reboot
============================
Check each path for suspicious init6 and reboot
==1. Stop the crond service==
# service crond stop
- Find directories that may store scheduled tasks
/var/spool/cron (Note: Move it away first)
/etc/cron.d (Note: Related to cron.hourly)
/etc/cron.weekly
/etc/cron.daily
/etc/cron.monthly
- Check and if no problems are found, restart the crond service
# service crond start
Redirecting to /bin/systemctl start crond.service
# ps aux|grep crond
root 1506 3.6 0.1 36304 3656 ? Ss 16:54 0:00 /usr/sbin/crond -n
root 1511 0.0 0.0 12320 1052 pts/0 S+ 16:54 0:00 grep --color=auto crond
#
If there are indeed any
# tail -f /var/log/cron (Log file)
(Note: Troubleshoot through the log)
=============================
The central location for scheduled tasks is /var/spool/cron/
# cd /var/spool/cron/
# ls
cali liangluyao root xull
# cat cali
……(Check for any suspicious entries, vim comment them all out)
Or move all files away, as follows
# ls
cali liangluyao root xull
# mkdir /backup (Note: Create a /backup directory under root) /backup backup directory
mv * /backup/ (Move files cali liangluyao root xull to /backup directory)
Scheduled tasks stopped, crond will not execute
# cd /backup/
# ls
cali liangluyao root xull
# cat cali (Note: After moving the scheduled task files away, check each one in the /backup directory)
……
# cat liangluyao
……
# w (Note: Check which users are logged in)
=======================================
Ride
# cd /etc/cron.daily
# ls
# vim …
# cd /etc/cron.hourly/
# cd /etc/cron.monthly
# cd /etc/cron.weekly
# cd /etc/cron.d (Note: Executes at the first minute of every hour)
(Note: run-parts is a command that runs all files in this folder)
=======================================================
Boot startup process
# cd /root (Note: In the root user's home directory)
# ls -a (Note: Hidden files)
# vim .bashrc
# vim .bash_profile
# vim /etc/profile (Note: Can quickly filter search for /reboot /init)
# vim /etc/bashrc (Note: Can quickly filter search for /reboot /init)
# cat /etc/rc.local
===============================================================================
IV. Variable Definition and Usage#
Main content:
Variable naming: 1. Underscore naming method (recommended in shell) 2. Camel case naming method 3. Hungarian naming method
Notes on variable naming
In shell, if a variable is not defined, it outputs an empty value
==env== View ==environment variables== in shell
==set== View ==all variables== in the current shell
==export outputs the variable as a global variable==
# ==echo represents the current bash process ID)==
# ==bash (Note: Start a child process, child bash)==
# ==echo "$mv $sg hello" (Note: When outputting a string of content, use double quotes)==
# ==echo '$mv $sg hello' (Note: Single quotes: what you see is what you get; $ followed by variable name will not reference the variable's value)==
Shell Programming
1. Variable Definition
variable Variable's
sg=tanglf
Variable Naming:
1. Underscore Naming Method (recommended in shell)
sc_sg=tanglf
2. Camel Case Naming Method
ScSg=wangtc
Small Camel
scSg=wangtc
Big Camel
ScSg=wangtc
3. Hungarian Naming Method
Notes on Variable Naming:
1. Cannot start with a number
2. Cannot be all numbers
3. Cannot contain special symbols #!@^$<> etc.
4. No spaces around the = sign
Environment variables in the Linux system are all uppercase
Custom variables should use lowercase as much as possible
Variables should generally be defined before use
In shell, if a variable is not defined, it outputs an empty value
# sc_sg=zhangxueyou
# echo $sg_sg
==(←Note: The variable sg_sg is not defined, output is empty)==
# echo $sc_sg
zhangxueyou
# ==echo $sgsg/$mvmv==
==/ (←Note: The variables {sgsg} {mvmv} are not defined, output is “/” root)==
#
# rm -rf ==$sgsg/$mvmv==
rm: Recursive operation on '/' is very dangerous
rm: Use the --no-preserve-root option to skip safety mode
#
==env== View ==environment variables== in shell
==set== View ==all variables== in the current shell
Variables have scope
Global variables
Local variables
==export outputs the variable as a global variable==
==Usage: Define first then export==
SSH access to port 22, sshd listens on port 22
Web service is on port 80
# ==echo represents the current bash process ID)==
1425
#
# ==export sg (Note: Export the variable sg as a global variable)==
# echo $$
==1425==
# mv=xull
# ==bash (Note: Start a child process, child bash)==
# echo $mv
==(Note: Output is empty, variable is not inherited)==
# echo $$
==1598==
# mv=xull
# ==export mv (Note: Export the variable as a global variable) (Note: export output)==
# ==bash (Note: Start a child process, child bash)==
# echo $mv
Xull ==(Note: The child process inherited the variable)==
#
# vim /root/.bash_profile
==When outputting a string of content, use double quotes==
==Single quotes: what you see is what you get; $ followed by variable name will not reference the variable's value==
# echo =="$mv $sg hello" (Note: When outputting a string of content, use double quotes)==
xull wangtc hello
# echo =='$mv $sg hello' (Note: Single quotes: what you see is what you get; $ followed by variable name will not reference the variable's value)==
$mv $sg hello
#