Linux features-Implement one-time tasks

Source: Internet
Author: User

First, the definition

At command: A timed task that specifies a time to perform a task that can only be performed once.

Second, the option

Iii. examples

1. Use the student user, create a task, execute after 2 minutes, append a row in/home/student/at.txt (created if not present), the content is the date command output result

[Email protected] ~]$ at now +2minutes
At> echo $ (date) >>/home/student/at.txt
At> <EOT>
Job at Thu Nov 3 11:20:00 2016

2. Check the student user's task list with ATQ to see the tasks in the previous step

[Email protected] ~]$ ATQ
10Thu 3 11:20:00 A student

3. Delete the task created in the previous step and view

Ten~

4. Switch to the root user, create a one-time task for the root user, print "$USER $ (date)" Output appended to the/home/student/at.txt file

Then go back to the student user, use ATQ to check the task, he is unable to see root (Other user's) task

 [[email protected] ~]# at now +2minutesat  > Echo $USER $ (date) > >/home/student/at.txtat  > <eot>job  11  at Thu Nov 3  11 : 23 : 00  2016  [ro[email protected]  ~]# su- student last Login:thu Nov  3  10
    :29 : 07  CST 2016  on Pts/0  [[email protected]  ~]$ atq[[email  Protected]  ~]$ 

5. Student also creates a task that executes after 2 minutes, performs the same actions as step 4, and now 2 users (root and student) have tasks waiting to be executed

[[email protected] ~]$ at now +2minutesat> Echo $USER $ (date) >>/home/student/at.txtat> &L T eot> at Thu  3: £ º

6. Perform the Atq check task with root user, the root user can see all the user's tasks

[Email protected] ~]# ATQ  One    Thu Nov  3: a rootof Thu-    Nov  3: Astudent

7. Back to the student user, the user can only view their own tasks

[Email protected] ~]$ ATQ  a    Thu Nov  3: Astudent

8 Switch to the root user, delete the 2 tasks created earlier, make sure the at list is empty

*

9. Using the student user, create a task to execute the reboot command on December 31, 10 o'clock in the morning this year

 to  . at > rebootat> <EOT>:00   .

10. Use student user, create a task, execute after 10 hours, restart sshd service

[Email protected] ~]$ at now +10hoursat> systemctl restart Sshdat> <EOT>16< /c3> at Thu  3: £ º

11. Use student user, create a task, 3 days after the 10 o'clock in the afternoon execution, restart the RSYSLOGD service

[Email protected] ~]$ at 10pm +3daysat> systemctl restart rsyslogd.serviceat> <EOT> /c2> at Sun Nov  6:xx:

12. Use student user, create a task, December 31, 2016 (denoted by mmddccyy) 23:59 points, execute poweroff command

:12312016at > poweroffat> <EOT>  to at::

13. Check the execution script of the last task and see the last shell command

[Email protected] ~]$ at-c -#!/bin/sh# atrun uid= +Gid= +# Mail Root0umask2Xdg_vtnr=1; export xdg_vtnrxdg_session_id=1; export Xdg_session_idhostname=desktop1.example.com; export Hostnameshell=/bin/bash; export shellhistsize= +; export Histsizeuser=student; export Userls_colors=rs=0:d i= -\;5\; -: ln= -\;5\;Wuyi: mh= -\; -\;5\; the:p i= +\; -\;5\; One: so= -\;5\; -: Do= -\;5\;5: bd= -\;5\;232\; -\;5\; One: cd= -\;5\;2
...
8\;5\; $: \*.oga= -\;5\; $: \*.spx= -\;5\; $: \*.xspf= -\;5\; $:; Export Ls_colorsmail=/var/spool/mail/student; export Mailpath=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/student/.local/bin:/home/student/bin; export Pathpwd=/home/student; export Pwdlang=en_us. utf-8; export Langhistcontrol=ignoredups; export HISTCONTROLSHLVL=1; export Shlvlxdg_seat=seat0; export Xdg_seathome=/home/student; export Homelogname=student; export Lognamelessopen=\|\|/usr/bin/lesspipe.sh\%s; export LESSOPENCD/home/student | |{echo'Execution Directory Inaccessible'>&2Exit1}${shell:-/BIN/SH} <<'marcindelimiter5d48d7d0'poweroffmarcindelimiter5d48d7d0

14. Check the/var/spool/at directory to see the above script

[email protected] ~]# LL/var/spool/at/ Total --RWX------.1Student Student2972Nov3  One: +a0000f01792f38-RWX------.1Student Student2988Nov3  One: AA000100177ebb5-RWX------.1Student Student theNov3  One: thea000110177fca8-RWX------.1Student Student2974Nov3  One: $a000120179327fdrwx------.2Daemon Daemon6Nov3  One: -Spool[[email protected]~]#

15. Create a Y-queue task with a time of 3 minutes (indicated by the exact time as 11:55), append the y character to the/home/student/at.txt file

All:at > Echo Y >>/home/student/at.txtat> <EOT>   at Thu Nov  3: 

16. Create an X queue task, the time is exactly the same as the previous task, append x characters to the/home/student/at.txt file, simulate 2 tasks at the same time to execute

All:at > Echo X >>/home/student/at.txtat> <EOT>   at Thu Nov  3: 

17. Check 2 tasks with ATQ, confirm that the time is the same, the queue is Y and X

[[Email protected] ~]# ATQ theSat Dec to Ten:xx:xx  .a student -Thu Nov3  +: A:xx  .a student -Sun Nov6  A:xx:xx  .a student -Sat Dec to  at: -:xx  .a student +Thu Nov3  One: -:xx  .y root -Thu Nov3  One: -:xx  .X root

18. Wait for the X and Y queue to execute after 2 tasks (after their execution time), check the/home/student/at.txt file with the tail command, see X on top,

Y below, which shows the X queue executed first executes the Y queue (there are 2 tasks at the same time, the letter of the queue is preceded by the first execution)

[Email protected] ~]# tail/home/student/3: £ º

19. With root user, edit/etc/at.deny so that student users do not have permission to create one-time tasks

" Student " >>/etc/~]# Su- student last Login:thu Nov  3: c9>00 on pts/0~]$ @ now + do

20. With root user, create and Boundary/etc/at.allow, grant student user permission to create one-time tasks,

Now student can be used at command, because At.allow takes precedence over At.deny

[Email protected] ~]# Echo"Student">>/etc/At.allow[[email protected]~]# Su-student last Login:thu Nov3  A: -: -Cst .On pts/0[[Email protected]~]$ at now +3hoursat> <EOT>Job AAt Thu Nov3  the: the:xx  .[[Email protected]~]$

Linux features-Implement one-time tasks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.