Sunday, July 27, 2014

Unix Shell: Process Schedule(1)

1. sleep command
sleep command allows the process wait for specified seconds and then continue running. During this period, the process doesn't consume any CPU resources.

script_2:
 #! /bin/bash  
   
 a=1  
 while [ $a -ne 0 ]  
 do  
   ((a++))  
   sleep 5  
 done  

terminal:
1) Launch the script_2 process at the background
2) run ps command to list running processes, script_2 is alive now.
3) run "ps aux" to list the header line
4) run "ps aux" to grep the script_2 line, it indicates that script_2 process in sleeping only occupies 0.2% CPU resources.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_2 &  
 [2] 5293  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ps  
  PID TTY     TIME CMD  
  2833 pts/1  00:00:01 bash  
  2968 pts/1  00:00:32 emacs  
  5293 pts/1  00:00:00 script_2  
  5294 pts/1  00:00:00 sleep  
  5295 pts/1  00:00:00 ps  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ps aux | head -n 1  
 USER    PID %CPU %MEM  VSZ  RSS TTY   STAT START  TIME COMMAND  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ps aux | grep script_2  
 aubinxia 5293 0.2 0.0  5296 1324 pts/1  S  13:33  0:00 /bin/bash ./script_2  
 aubinxia 5302 0.0 0.0  4680  820 pts/1  S+  13:33  0:00 grep --color=auto script_2  


2. at command
Given a script, specify it to run on schedule.

script_1:
 #! /bin/bash  
   
 echo "Hello world!" > temp.txt  

terminal:
1) Run script immediately. And we get the output saying the script is scheduled to be running at 14:16
2) After 14:16, list temp.txt, last modification time is exactly 14:16
3) Run script 1 minute away from now. And we get the output saying the script is scheduled to be running at 14:18
4) After 14:18, list temp.txt last modification time is exactly 14:18, meaning it is just generated.
5) Run script 2 hours from now, and we get the output saying the script is scheduled to be running 2 hours later.
6) Run script 1 day from now, and we get the output confirming that.
7) Run script 1 month from now, and we get the output confirming that.
8) Run script 1 year from now, and we get the output confirming that.
9) at -l option could help us review all scheduled jobs right now. The first number is the job# used to identify the scheduled job.

 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ at now -f ./script_1  
 warning: commands will be executed using /bin/sh  
 job 6 at Sun Jul 27 14:16:00 2014  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -lrt temp.txt  
 -rw-rw-r-- 1 aubinxia aubinxia 13 Jul 27 14:16 temp.txt  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ at now + 1 minutes -f ./script_1  
 warning: commands will be executed using /bin/sh  
 job 7 at Sun Jul 27 14:18:00 2014  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -lrt temp.txt  
 -rw-rw-r-- 1 aubinxia aubinxia 13 Jul 27 14:18 temp.txt  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ at now + 2 hours -f ./script_1
 warning: commands will be executed using /bin/sh
 job 8 at Sun Jul 27 16:20:00 2014
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ at now + 1 days -f ./script_1
 warning: commands will be executed using /bin/sh
 job 9 at Mon Jul 28 14:21:00 2014
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ at now + 1 months -f ./script_1
 warning: commands will be executed using /bin/sh
 job 10 at Wed Aug 27 14:21:00 2014
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ at now + 1 years -f ./script_1
 warning: commands will be executed using /bin/sh
 job 12 at Mon Jul 27 14:21:00 2015
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ at -l
 8 Sun Jul 27 16:20:00 2014 a aubinxia
 12 Mon Jul 27 14:21:00 2015 a aubinxia
 9 Mon Jul 28 14:21:00 2014 a aubinxia
 11 Mon Jul 27 14:21:00 2015 a aubinxia
 10 Wed Aug 27 14:21:00 2014 a aubinxia

terminal:
1) Wrong input, since if using pm as suffix, then hour number should be 01 - 12
2) Ignore the pm suffix, then it is good.
3) Or we use pm suffix, but write the correct hour number
4) Schedule the process to be running at 07/28 2:00pm this year.
5) Schedule the process to be running at 07/28 2:00pm next year: 2015.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ at 14:00pm -f ./script_1  
 Hour too large for PM. Last token seen: pm  
 Garbled time  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ at 14:00 -f ./script_1  
 warning: commands will be executed using /bin/sh  
 job 13 at Mon Jul 28 14:00:00 2014  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ at 02:00pm -f ./script_1  
 warning: commands will be executed using /bin/sh  
 job 14 at Mon Jul 28 14:00:00 2014  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ at 02:00pm 28 July -f ./script_1  
 warning: commands will be executed using /bin/sh  
 job 15 at Mon Jul 28 14:00:00 2014  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ at 02:00pm 28 July 2015 -f ./script_1  
 warning: commands will be executed using /bin/sh  
 job 16 at Tue Jul 28 14:00:00 2015  

terminal:
1) Use -l option to list all pending jobs
2) use atrm 16 to remove the job 16
3) list the pending jobs again, job 16 is already removed.
4) use -r option(same as atrm) ot remove job 15
5) list the pending jobs again, job 15 is already removed.
6) run atq command to list all pending jobs(same as at -l)
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ at -l | sort -k1n  
 8    Sun Jul 27 16:20:00 2014 a aubinxia  
 9    Mon Jul 28 14:21:00 2014 a aubinxia  
 10    Wed Aug 27 14:21:00 2014 a aubinxia  
 11    Mon Jul 27 14:21:00 2015 a aubinxia  
 12    Mon Jul 27 14:21:00 2015 a aubinxia  
 13    Mon Jul 28 14:00:00 2014 a aubinxia  
 14    Mon Jul 28 14:00:00 2014 a aubinxia  
 15    Mon Jul 28 14:00:00 2014 a aubinxia  
 16    Tue Jul 28 14:00:00 2015 a aubinxia  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ atrm 16  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ at -l | sort -k1n  
 8    Sun Jul 27 16:20:00 2014 a aubinxia  
 9    Mon Jul 28 14:21:00 2014 a aubinxia  
 10    Wed Aug 27 14:21:00 2014 a aubinxia  
 11    Mon Jul 27 14:21:00 2015 a aubinxia  
 12    Mon Jul 27 14:21:00 2015 a aubinxia  
 13    Mon Jul 28 14:00:00 2014 a aubinxia  
 14    Mon Jul 28 14:00:00 2014 a aubinxia  
 15    Mon Jul 28 14:00:00 2014 a aubinxia  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ at -r 15  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ at -l | sort -k1n  
 8    Sun Jul 27 16:20:00 2014 a aubinxia  
 9    Mon Jul 28 14:21:00 2014 a aubinxia  
 10    Wed Aug 27 14:21:00 2014 a aubinxia  
 11    Mon Jul 27 14:21:00 2015 a aubinxia  
 12    Mon Jul 27 14:21:00 2015 a aubinxia  
 13    Mon Jul 28 14:00:00 2014 a aubinxia  
 14    Mon Jul 28 14:00:00 2014 a aubinxia  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ atq  
 8    Sun Jul 27 16:20:00 2014 a aubinxia  
 12    Mon Jul 27 14:21:00 2015 a aubinxia  
 14    Mon Jul 28 14:00:00 2014 a aubinxia  
 9    Mon Jul 28 14:21:00 2014 a aubinxia  
 13    Mon Jul 28 14:00:00 2014 a aubinxia  
 11    Mon Jul 27 14:21:00 2015 a aubinxia  
 10    Wed Aug 27 14:21:00 2014 a aubinxia  

No comments:

Post a Comment