Detailed explanation of scheduled tasks for ordinary users in Linux

Detailed explanation of scheduled tasks for ordinary users in Linux

Preface

Ordinary users define crontab scheduled tasks: For example, an oracle user defines a scheduled task: print the current directory every minute

[oracle@node2 ~]$ crontab -e

*/1 * * * * /bin/ls -al > /tmp/ls.log

How to determine whether a scheduled task has been executed? First, the crond service must be running

[oracle@node2 ~]$ service crond status
Redirecting to /bin/systemctl status crond.service
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2018-07-15 08:27:38 EDT; 2min 43s ago
Main PID: 6189 (crond)
CGroup: /system.slice/crond.service
└─6189 /usr/sbin/crond -n
[oracle@node2 ~]$

1. Use the mail command as the root user

[root@node2 ~]# mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/root": 801 messages 9 new 94 unread
N795 (Cron Daemon) Sun Jul 15 08:25 35/932 "Cron <root@node2> /bin/ls"
N796 (Cron Daemon) Sun Jul 15 08:26 35/932 "Cron <root@node2> /bin/ls"
N797 (Cron Daemon) Sun Jul 15 08:27 35/932 "Cron <root@node2> /bin/ls"
N798 (Cron Daemon) Sun Jul 15 08:28 35/932 "Cron <root@node2> /bin/ls"
N799 (Cron Daemon) Sun Jul 15 08:29 35/932 "Cron <root@node2> /bin/ls"
N800 (Cron Daemon) Sun Jul 15 08:30 35/932 "Cron <root@node2> /bin/ls"
& file
"/var/spool/mail/root": 801 messages 9 new 94 unread
& h
>N793 (Cron Daemon) Sun Jul 15 08:23 35/932 "Cron <root@node2> /bin/ls"
N794 (Cron Daemon) Sun Jul 15 08:24 35/932 "Cron <root@node2> /bin/ls"
N795 (Cron Daemon) Sun Jul 15 08:25 35/932 "Cron <root@node2> /bin/ls"
N796 (Cron Daemon) Sun Jul 15 08:26 35/932 "Cron <root@node2> /bin/ls"
N797 (Cron Daemon) Sun Jul 15 08:27 35/932 "Cron <root@node2> /bin/ls"
N798 (Cron Daemon) Sun Jul 15 08:28 35/932 "Cron <root@node2> /bin/ls"
N799 (Cron Daemon) Sun Jul 15 08:29 35/932 "Cron <root@node2> /bin/ls"
N800 (Cron Daemon) Sun Jul 15 08:30 35/932 "Cron <root@node2> /bin/ls"
&800
Message 800:
From [email protected] Sun Jul 15 08:30:01 2018
Return-Path: <[email protected]>
X-Original-To: root
Delivered-To: [email protected]
From: "(Cron Daemon)" <[email protected]>
To: [email protected]
Subject: Cron <root@node2> /bin/ls
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=278>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/0>
X-Cron-Env: <LANG=en_US.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Date: Sun, 15 Jul 2018 08:30:01 -0400 (EDT)
Status: R
\
anaconda-ks.cfg
Desktop
Documents
Downloads
initial-setup-ks.cfg
Music
Pictures
Public
Templates
Videos

In the mial interactive environment, use the file command to display the total number of current emails and other information. Use head to view the most recent email and enter the number to view the detailed information of the email.

2. View the log: tail -n 10 /var/log/cron

[root@node2 ~]# tail -n 10 /var/log/cron
Jul 15 08:31:01 node2 CROND[6297]: (root) CMD (/bin/ls)
Jul 15 08:31:01 node2 CROND[6298]: (oracle) CMD (/bin/ls -al > /tmp/ls.log )
Jul 15 08:32:01 node2 CROND[6321]: (oracle) CMD (/bin/ls -al > /tmp/ls.log )
Jul 15 08:32:01 node2 CROND[6322]: (root) CMD (/bin/ls)
Jul 15 08:33:02 node2 CROND[6342]: (root) CMD (/bin/ls)
Jul 15 08:33:02 node2 CROND[6343]: (oracle) CMD (/bin/ls -al > /tmp/ls.log )
Jul 15 08:34:01 node2 CROND[6362]: (root) CMD (/bin/ls)
Jul 15 08:34:01 node2 CROND[6363]: (oracle) CMD (/bin/ls -al > /tmp/ls.log )
Jul 15 08:35:01 node2 CROND[6382]: (oracle) CMD (/bin/ls -al > /tmp/ls.log )
Jul 15 08:35:01 node2 CROND[6383]: (root) CMD (/bin/ls)
[root@node2 ~]#

The log file records the execution of scheduled tasks for all users

3. Use mail to view under ordinary user

(In the scheduled task, the user who executes the scheduled task must be clearly defined, otherwise even if the scheduled task is defined under the oracle user, the email may not be received)

[oracle@node2 ~]$ crontab -l
*/1 * * * * oracle /bin/ls -al > /tmp/ls.log
[oracle@node2 ~]$
[oracle@node2 ~]$ mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/oracle": 6 messages 1 unread
1 (Cron Daemon) Mon Mar 5 14:10 52/2476 "Cron <oracle@localhost> ls -al"
2 (Cron Daemon) Sun Jul 15 06:10 57/2777 "Cron <oracle@node2> /bin/ls -al"
3 (Cron Daemon) Sun Jul 15 06:11 57/2777 "Cron <oracle@node2> /bin/ls -al"
4 (Cron Daemon) Sun Jul 15 06:12 57/2778 "Cron <oracle@node2> /bin/ls -al"
5 (Cron Daemon) Sun Jul 15 06:13 57/2778 "Cron <oracle@node2> /bin/ls -al"
>U 6 (Cron Daemon) Sun Jul 15 08:38 26/929 "Cron <oracle@node2> oracle /bin/ls -al > /tmp/ls.log"
&

The /var/log/cron file cannot be directly viewed by ordinary users

[oracle@node2 ~]$ tail -n 10 /var/log/cron
tail: cannot open '/var/log/cron' for reading: Permission denied
You have new mail in /var/spool/mail/oracle
[oracle@node2 ~]$

There is another way to write scheduled tasks in the /etc/crontab file, but in centos7, writing and executing in this file will result in an error

[oracle@node2 ~]$ cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- 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
[oracle@node2 ~]$

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • How to use cron to execute tasks regularly in Linux
  • Linux crontab scheduled task configuration method (detailed explanation)
  • Linux uses crontab to implement PHP execution plan timing tasks
  • The server regularly executes scheduled tasks and regularly accesses pages (windows/linux)
  • How to use crontab to execute a scheduled task once a second in Linux
  • Detailed explanation of how to use PHP to schedule cron tasks in Linux
  • How to execute tasks regularly under Linux and instructions on how to use crontab (collected and sorted)
  • Detailed explanation of Python script self-starting and scheduled tasks under Linux
  • Linux uses scheduled tasks to clean up logs 45 days ago every week
  • Linux scheduled task access URL example

<<:  Exploration and correction of the weird behavior of parseInt() in js

>>:  Basic learning and experience sharing of MySQL transactions

Recommend

Linux kernel device driver kernel debugging technical notes collation

/****************** * Kernel debugging technology...

ftp remotely connect to Linux via SSH

First install ssh in Linux, taking centos as an e...

Mysql case analysis of transaction isolation level

Table of contents 1. Theory SERIALIZABLE REPEATAB...

CSS form validation function implementation code

Rendering principle In the form element, there is...

Implementation steps of vue-element-admin to build a backend management system

Recently, when I was working on a conference heal...

PostgreSQL materialized view process analysis

This article mainly introduces the process analys...

Two ways to completely delete users under Linux

Linux Operation Experimental environment: Centos7...

Let's talk briefly about the changes in setup in vue3.0 sfc

Table of contents Preface Standard sfc writing me...

Solve the problem of garbled Chinese characters in Mysql5.7

When using MySQL 5.7, you will find that garbled ...

jQuery implements the drop-down box for selecting the place of residence

The specific code for using jQuery to implement t...

Detailed explanation of WeChat Mini Program official face verification

The mini program collected user personal informat...

CSS scroll-snap scroll event stop and element position detection implementation

1. Scroll Snap is a must-have skill for front-end...

Practical example of Vue virtual list

Table of contents Preface design accomplish summa...

Linux Cron scheduled execution of PHP code with parameters

1. Still use PHP script to execute. Command line ...

Vue-cli creates a project and analyzes the project structure

Table of contents 1. Enter a directory and create...