Advertisement
sandervanvugt

Untitled

Aug 7th, 2020
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.56 KB | None | 0 0
  1. ### script 1 improvements
  2. [root@linfun2 cool-bash]# cat 1_cpu-hog
  3. #!/bin/bash
  4. # Script that monitors the top-active process. The script sends an email to the user root if
  5. # utilization of the top active process goed beyond 80%. Of course, this script can be tuned to
  6. # do anything else in such a case.
  7. #
  8. # Start the script, and it will run forever.
  9.  
  10. while true
  11. do
  12. # Check every 60 seconds if we have a process causing high CPU load
  13. PUSAGE=$(ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1)
  14. USAGE1=$(echo $PUSAGE | awk '{ print $1 }')
  15. USAGE1=${USAGE1%.*}
  16. PID1=$(echo $PUSAGE | awk '{ print $2 }')
  17. PNAME1=$(echo $PUSAGE | awk '{ print $3 }')
  18.  
  19.  
  20. # Only if we have a high CPU load on one process, run a check within 7 seconds
  21. # In this check, we should monitor if the process is still that active
  22. # If that's the case, root gets a message
  23. if [ $USAGE1 -gt 80 ]
  24. then
  25. sleep 7
  26. PUSAGE2=$(ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1)
  27. USAGE2=$(echo $PUSAGE2 | awk '{ print $1 }')
  28. USAGE2=${USAGE2%.*}
  29. PID2=$(echo $PUSAGE2 | awk '{ print $2 }')
  30. PNAME2=$(echo $PUSAGE2 | awk '{ print $3 }')
  31.  
  32.  
  33. # Now we have variables with the old process information and with the
  34. # new information
  35.  
  36. [ $USAGE2 -gt 80 ] && [ $PID1 = $PID2 ] && mail -s "CPU load of $PNAME is above 80%" root@blah.com < .
  37. fi
  38. sleep 6
  39. done
  40.  
  41.  
  42. ### AW long break solution
  43. root@linfun2 ~]# cat countdown2
  44. #!/bin/bash
  45.  
  46. COUNTER=$1
  47. COUNTER=$(( COUNTER * 60 ))
  48.  
  49. while true
  50. do
  51. echo $(($COUNTER/3600))' hours' $(($COUNTER%3600/60))' mins'$(($COUNTER%60))' secs remaining in break'
  52. COUNTER=$((COUNTER-1))
  53. sleep 1
  54. done
  55.  
  56.  
  57. #### SC solution to assignment 2
  58.  
  59. [root@linfun2 cool-bash]# cat sc2
  60. #!/bin/bash
  61.  
  62. set -u
  63.  
  64. arg=$1
  65.  
  66. test -n $arg || { echo "missing arg" 1>&2; exit 1; }
  67.  
  68. if test -f $arg
  69. then : file
  70. vim $arg
  71. elif test -d $arg
  72. then : directory
  73. cd $arg
  74. exec bash
  75. else : other
  76. echo "unknown type" 1>&2
  77. exit 2
  78. fi
  79.  
  80.  
  81. ### GROUPCHAT
  82. Nurul I
  83. Hello everyone, welcome to the course Writing Cool Bash Shell Scripts in 3 Hours with Sander van Vugt. We hope you enjoy the course!
  84.  
  85. Nurul I
  86. Please Note: The Group Chat will be part of the recording so please do not post personal information (such as email addresses) or anything confidential.
  87.  
  88. Nurul I
  89. We will begin the course in approximately 14 minutes
  90.  
  91. . h
  92. metaphorically twiddles thumbs while listening to Mozart
  93.  
  94. Nurul I
  95. The course has started, you should all be hearing Sander now
  96.  
  97. s p
  98. Hi All, Good Morning
  99.  
  100. k k
  101. morning
  102.  
  103. s p
  104. I d idnt get any mail about Replay of the Unix session had on 3rd Aug.. any thoughts pls
  105.  
  106. D S
  107. Average experience
  108.  
  109. A A
  110. Good morning all
  111.  
  112. Nurul I
  113. sp, please email our customer service team for details, the team will be able to help- onlinetraining@oreilly.com. Thank you!
  114.  
  115. P N
  116. Will it be possible to share earlier classes scripting examples?
  117.  
  118. F S
  119. please show with production senario
  120.  
  121. C L
  122. Hi, the "On which OS platform are you planning to use Bash shell scripts" question could perhaps be multiple-choice?
  123.  
  124. P N
  125. We didn't had option to select both and Linux :-)
  126.  
  127. E D
  128. @PN Bash Scripting 4 Hours repo should be publicly available at the Sander's github
  129.  
  130. P N
  131. both Mac and Linux
  132.  
  133. E D
  134. named bash-scripting
  135.  
  136. F S
  137. online katakoda terminal
  138.  
  139. J S
  140. katacoda is good
  141.  
  142. P N
  143. Got it - https://github.com/sandervanvugt/bash-scripting
  144.  
  145. J K
  146. git clone https://github.com/sandervanvugt/cool-bash.git
  147.  
  148. sander v
  149. pastebin.com
  150.  
  151. D S
  152. Yes
  153.  
  154. P N
  155. @JK, I was referring to earlier class code of 4 hours... For present one is the one that you shared.
  156.  
  157. T C
  158. @SP Are you referring to the Aug "Bash Shell Scripting in 4 Hours"? If so, the link you seek is the same link, in the reminder email, that sent to us before training begins. Hope that helps.
  159.  
  160. s p
  161. yes
  162.  
  163. s p
  164. I didnt get any reminder mails ..
  165.  
  166. s p
  167. Thanks for reply
  168.  
  169. S C
  170. missing close back-tick on line 29
  171.  
  172. g v
  173. hey guys whats the username for github?
  174.  
  175. A W
  176. no username
  177.  
  178. S C
  179. you don't need one
  180.  
  181. g v
  182. ok
  183.  
  184. J K
  185. install git
  186.  
  187. J K
  188. git clone https://github.com/sandervanvugt/cool-bash.git
  189.  
  190. g v
  191. thanks
  192.  
  193. J K
  194. sleep 60 should be in the else block ?
  195.  
  196. G S
  197. or download https://github.com/sandervanvugt/bash-scripting/archive/master.zip and then unzip if you don't have git
  198.  
  199. E D
  200. Expected behavior is to run only once every 60 seconds.
  201.  
  202. E D
  203. @JK
  204.  
  205. L L
  206. There seems to be an error in the script. I just clone the repo, and I'm getting:
  207.  
  208. J K
  209. Yeah I know
  210.  
  211. L L
  212. $ bash 1_cpu-hog 1_cpu-hog: line 29: unexpected EOF while looking for matching ``' 1_cpu-hog: line 37: syntax error: unexpected end of file
  213.  
  214. J K
  215. But it then block has sleep 7
  216.  
  217. J K
  218. *if then block
  219.  
  220. J K
  221. If CPU usage > 80 then run every 7 seconds, else run every 60 seconds
  222.  
  223. J K
  224. He asked what is one error in this code so I was trying to estimate
  225.  
  226. A R
  227. weird apostrophes
  228.  
  229. J K
  230. that executes the command
  231.  
  232. G S
  233. Missing backtick
  234.  
  235. E D
  236. Oh I see, of course it'll not loop in the if block. I agree with you
  237.  
  238. c O
  239. instead of while true a cron job may be used?
  240.  
  241. S C
  242. better an atjob than a cronjob
  243.  
  244. L L
  245. There seems to be an error in line #29. missing ending backtick.
  246.  
  247. M R
  248. I would grab awk $1 ";" $2,";" $3 for ps -eo into one variable and then grab out each result, that way it is for the same ps -eo iteration.
  249.  
  250. J K
  251. Agree with MR
  252.  
  253. L L
  254. @MR, Is this possible with bash? Get $1, $2, $3 at the same time?
  255.  
  256. S C
  257. @LL use eval
  258.  
  259. K E
  260. PNAME2 should end with `
  261.  
  262. L L
  263. I wonder if double square bracket [[ ]] would be a better choice over single square bracket [ ]?
  264.  
  265. E D
  266. In terms of readability?
  267.  
  268. s p
  269. Can we please get copy of the Group chat dump
  270.  
  271. T Y
  272. Could probably run ps command once instead of 3 times to get usage, pid, pname
  273.  
  274. S C
  275. would normally redirect /dev/null into mail command
  276.  
  277. s p
  278. or keep in repository location
  279.  
  280. H B
  281. ps should be used with --no-headers
  282.  
  283. E D
  284. Group chat is being recored, you can view from recording later on
  285.  
  286. S C
  287. I wouldn't want to be flooded with email every 60s !
  288.  
  289. T C
  290. Agreed SC...that would be overkill!
  291.  
  292. T I
  293. a cosmetic comment, I never use var1 or var2 or var3. The usage of descriptive variable names is clear to know what's happening too.
  294.  
  295. N R
  296. in same script will give awk '{print $1 $2 $3}'
  297.  
  298. M R
  299. the sleep 60 is too early
  300.  
  301. B G
  302. 60 secs sleep?
  303.  
  304. j m
  305. it's sleeping for 60 sec
  306.  
  307. C M
  308. sleep 60 should be at the end maybe
  309.  
  310. C M
  311. so the first run always happens first
  312.  
  313. G S
  314. Agreed, ideally the 60 second sleep would be at the end of the loop.
  315.  
  316. T C
  317. Yup TI...no descriptive variables makes it more challenging to debug someone else's code snippet.
  318.  
  319. E D
  320. Sleep 60 is ok if you ask my opinion, however, it should check the time passed since the last mail it sent, to address the problem @SC pointed out
  321.  
  322. B G
  323. multivariable assignment
  324.  
  325. J G
  326. use one line to gather the wanted values and separate their values from that
  327.  
  328. S C
  329. set `ps -eo pcpu,pid -o comm=`
  330.  
  331. B G
  332. but how is it done in bash?
  333.  
  334. B B
  335. I get this: ./1_cpu-hog: line 20: [: 13,2: integer expression expected
  336.  
  337. C M
  338. get all the fields in one awk
  339.  
  340. N R
  341. will keep usage pid pname in single script
  342.  
  343. G S
  344. Capture all the results at once so there's not mutability between execs
  345.  
  346. S C
  347. USAGE=$1
  348.  
  349. D S
  350. Capture process list results.
  351.  
  352. B B
  353. capture usaage Pid name in awk command $1,$2, $3
  354.  
  355. T Y
  356. get all 3 as a single string and then split them
  357.  
  358. S C
  359. PID=$2
  360.  
  361. P T
  362. captue results at once
  363.  
  364. S C
  365. etc
  366.  
  367. G S
  368. for the output of ps
  369.  
  370. D S
  371. Pull values from the process list.
  372.  
  373. D C
  374. break the utilisation logic into a function to make the script more readable
  375.  
  376. C M
  377. +1 on that ^^
  378.  
  379. D S
  380. I was going to mention that, isn't the new way $()
  381.  
  382. j m
  383. is it more efficient?
  384.  
  385. U 1
  386. what was the command run for creating a load of more than 80%
  387.  
  388. J K
  389. $() will not be compatible with older Unix systems?
  390.  
  391. U 1
  392. can anyone post
  393.  
  394. j m
  395. @U1 dd if=/dev/zero of=/dev/null
  396.  
  397. J K
  398. $@
  399.  
  400. J K
  401. Sorry...
  402.  
  403. J G
  404. USAGE1 not USAGE
  405.  
  406. L L
  407. Is = for equality test bash specific? I thought the "standard" for string equality test is ==
  408.  
  409. S C
  410. @LL it historically it used single =
  411.  
  412. J K
  413. Why do a string comparison?
  414.  
  415. S C
  416. [[ uses ==
  417.  
  418. J K
  419. Can you use $PID -eq $PID2 ?
  420.  
  421. L L
  422. @SC Thanks!
  423.  
  424. V E
  425. set :nu
  426.  
  427. M R
  428. you have PNAME
  429.  
  430. S C
  431. or (( PID == PPID2 ))
  432.  
  433. C M
  434. to get all 3 variables at once .. it should be something like this:
  435.  
  436. M S
  437. vim +21 filename to open vim on a specific line, here in line number 21
  438.  
  439. C M
  440. read var1 var2 <<< $(COMMAND | awk '{print $1,$2}')
  441.  
  442. P T
  443. you don't need to declare variable PID1 and PNAME1 if usage1 is gt 80
  444.  
  445. P T
  446. *if usage1 is not gt 80
  447.  
  448. S C
  449. @cm nice
  450.  
  451. N R
  452. else exit
  453.  
  454. C M
  455. yes .. let's move on to next
  456.  
  457. C M
  458. this is good
  459.  
  460. H B
  461. cron will start new process every time. You will need additional logic for avoiding multiple processes
  462.  
  463. sander v
  464. read var1 var2 <<< $(echo "$COMMAND | awk '{print $1,$2}')
  465.  
  466. H B
  467. read -a foo <<<"$(ps -eo pcpu,pid -o comm= --no-headers| sort -k1 -n -r |head -1|sed 's/ /& /g')"
  468.  
  469. H B
  470. if we want to use array
  471.  
  472. T K
  473. pastebin link ?
  474.  
  475. T Y
  476. 5 min or 12 min?
  477.  
  478. A W
  479. he said 12min
  480.  
  481. T Y
  482. @AW thanks. I thought I heard 5
  483.  
  484. A W
  485. :q!
  486.  
  487. A W
  488. wrong window
  489.  
  490. s p
  491. To chek the group need to view replay till end .. instead somehow if the group chat is captured in text files and placed in Repository .. it will give the convesration at a glance ..
  492.  
  493. s p
  494. for review - gives better readability also
  495.  
  496. J K
  497. I have minimal scripting experience form years ago. I watches the first 40 mins maybe of Sander's course on Bash scripting last night and was able to follow along
  498.  
  499. J K
  500. I do have extensive C# and some Linux experience.
  501.  
  502. J K
  503. I do not think that last script was too advanced, which is good or I would have been lost.
  504.  
  505. A W
  506. @JK sounds like you have a excellent understanding of techniques and could pick up just about any language
  507.  
  508. S C
  509. but this was supposed to be an advanced course
  510.  
  511. J K
  512. Advanced is a relative term I guess, lol.
  513.  
  514. S C
  515. :-)
  516.  
  517. C M
  518. true
  519.  
  520. E D
  521. @JK hehehe correct :)
  522.  
  523. K E
  524. while true // what is it checking against?
  525.  
  526. A R
  527. When does Sander give an hour long break?
  528.  
  529. J K
  530. 'true' is the condition
  531.  
  532. J K
  533. It will always be true and run forever
  534.  
  535. T K
  536. :) \
  537.  
  538. A R
  539. :D
  540.  
  541. S C
  542. for seq in `seq 1 720`; do sleep 1; done
  543.  
  544. A W
  545. have we restarted ? I can't hear anything - want to see if it is audio on my side
  546.  
  547. C M
  548. yes we have
  549.  
  550. A R
  551. We have
  552.  
  553. S C
  554. he's speaking
  555.  
  556. A W
  557. thanks
  558.  
  559. A R
  560. f5 your browser
  561.  
  562. s p
  563. refresh browser
  564.  
  565. A W
  566. thanks
  567.  
  568. S C
  569. should use +'%Y-%m-%d'
  570.  
  571. S C
  572. ISO standard!
  573.  
  574. S C
  575. lol
  576.  
  577. T Y
  578. # - left to right and % - right to left?
  579.  
  580. H F
  581. If you already have good grep is it worth learning this pattern matching?
  582.  
  583. S C
  584. or use 'expr'
  585.  
  586. T Y
  587. BLAH lol
  588.  
  589. T K
  590. i should start using it instead of temp :p
  591.  
  592. C T
  593. can we use "cut"
  594.  
  595. S K
  596. -
  597.  
  598. S K
  599. -F"-"
  600.  
  601. D S
  602. $() you need
  603.  
  604. F M
  605. replace "-" with space via sed | cut ? ugly but should work
  606.  
  607. S C
  608. cut -d "-" -f2
  609.  
  610. A D
  611. so finally which way is better , patren match,cut or awk ?
  612.  
  613. J K
  614. pattern
  615.  
  616. J K
  617. It uses only internal commands
  618.  
  619. D S
  620. Please show the command again.
  621.  
  622. A D
  623. ok thx
  624.  
  625. S C
  626. IFS="-" read TODAY THISMONTH YEAR <<<$(date +%Y-%m-%d)
  627.  
  628. T Y
  629. SC's version wins
  630.  
  631. D S
  632. he has Year Month Date in the incorrect order
  633.  
  634. S C
  635. true, soz
  636.  
  637. D S
  638. =)
  639.  
  640. J K
  641. first line is not shebang
  642.  
  643. J K
  644. which script?
  645.  
  646. T Y
  647. assignment in slides
  648.  
  649. J K
  650. tx
  651.  
  652. C M
  653. a script that those what is asked in assignement
  654.  
  655. S S
  656. ^d
  657.  
  658. J K
  659. what is :
  660.  
  661. J K
  662. Is that for comment?
  663.  
  664. B B
  665. what is : directory
  666.  
  667. S C
  668. its a comment which shows up when run via "bash -x" it helps to debug
  669.  
  670. J K
  671. why not exec before bash?
  672.  
  673. J K
  674. Tx SC
  675.  
  676. B B
  677. Thanks
  678.  
  679. S C
  680. that was my -u sorry
  681.  
  682. A W
  683. where do I see pastebin for this class?
  684.  
  685. B G
  686. end of class
  687.  
  688. A W
  689. Tx @BG
  690.  
  691. J K
  692. Are keywords like 'if', 'else' considered external to shell? I think not perhaps
  693.  
  694. S C
  695. builtin
  696.  
  697. A W
  698. Here is good list of file test operators --> https://linuxize.com/post/bash-check-if-file-exists/
  699.  
  700. U 1
  701. why was vi used here
  702.  
  703. U 1
  704. vi $1
  705.  
  706. B G
  707. the idea was that the argument should be passed either to vi or cd
  708.  
  709. U 1
  710. ok
  711.  
  712. L n
  713. SC can you share you script please?
  714.  
  715. S C
  716. https://pastebin.com/7zHwqGk6
  717.  
  718. L n
  719. Thank you ! :)
  720.  
  721. J K
  722. I am not familiar with pastebin. How long is that link valid?
  723.  
  724. G S
  725. @AW good list, but usually best one is with the man page for bash on your system. "man bash" and then search for "CONDITIONAL EXPRESSIONS", e.g., "/^COND"
  726.  
  727. J K
  728. Thanks @SC
  729.  
  730. s p
  731. @sc
  732.  
  733. B G
  734. 1 month unless you have a pro subscription I think
  735.  
  736. s p
  737. can u pls tell what 1>&2 does here
  738.  
  739. s p
  740. &
  741.  
  742. S C
  743. it redirects stdout to stderr
  744.  
  745. s p
  746. thanks
  747.  
  748. C M
  749. https://stackoverflow.com/questions/818255/in-the-shell-what-does-21-mean
  750.  
  751. s p
  752. Thnx
  753.  
  754. J K
  755. I have not seen that before. Why would you want to redirect stdout to stderr?
  756.  
  757. F M
  758. i have a script that reads in some values from the user and then compares those against valid values. depending on the environment the script is used in, valid values can change and should be set in a config file. how do you automate getting those from the
  759.  
  760. F M
  761. ... config
  762.  
  763. G S
  764. @JK if you want to pipe both stdout and stderr to a file or a pipe
  765.  
  766. S C
  767. you want good messages to go to stdout and error messages to stderr, they both go to the console, but tis good practice to differentiate
  768.  
  769. G S
  770. redirect to a file that is.
  771.  
  772. F M
  773. and those are "n" values, meaning the number of values change from environment to another
  774.  
  775. S C
  776. command1 |& command2
  777.  
  778. S C
  779. will pass both stdout and stderr to command2
  780.  
  781. S C
  782. otherwise stderr is not sent to it, just to the console
  783.  
  784. s p
  785. @Sc - will there be error file - any specific location
  786.  
  787. s p
  788. or same as in the current loc
  789.  
  790. J K
  791. okay, thanks @GS and @SC
  792.  
  793. s p
  794. Thanks @GS and @SC
  795.  
  796. F M
  797. you can append a line to .bashrc that is run after reboot
  798.  
  799. J K
  800. not, bash_profile ?
  801.  
  802. F M
  803. then make sure you remove that line after execution
  804.  
  805. S C
  806. dangerous
  807.  
  808. F M
  809. i agree
  810.  
  811. J K
  812. Oh, I get it. REBOOT is just a label, not an actual reboot since bash is case sensitive.
  813.  
  814. S C
  815. no
  816.  
  817. C M
  818. sara connor
  819.  
  820. J K
  821. ha ha
  822.  
  823. S C
  824. i am the terminator! ;-)
  825.  
  826. C M
  827. ha ha
  828.  
  829. J K
  830. He'll be back...
  831.  
  832. J K
  833. In other classes
  834.  
  835. T K
  836. :D
  837.  
  838. S C
  839. (y)
  840.  
  841. G S
  842. hasta la vista
  843.  
  844. T K
  845. What was export DIR in previous script ?
  846.  
  847. j m
  848. while true do dd if=/dev/zero of=/dev/null done
  849.  
  850. T Y
  851. I think it would be more useful if it was a slow ramp up
  852.  
  853. J K
  854. Very useful. We try to exhaust systems to see how our applications work with low resources.
  855.  
  856. J K
  857. notepad++
  858.  
  859. J K
  860. vim is the best.
  861.  
  862. j m
  863. vim
  864.  
  865. F M
  866. emacs
  867.  
  868. A S
  869. in intelliJ you have a plugin
  870.  
  871. B D
  872. VIM, or Visual Studio Code is a good free GUI
  873.  
  874. . h
  875. nevoid with plugins
  876.  
  877. B D
  878. also it's extremely useful to use shellcheck https://github.com/koalaman/shellcheck
  879.  
  880. . h
  881. sorry NeoVim
  882.  
  883. C M
  884. thanks a lot guys!!
  885.  
  886. T Y
  887. It's a trap!
  888.  
  889. . h
  890. NeoVim is Vim rewritten with a lot of the ancient cruft removed and is faster, more stable etc.
  891.  
  892. s p
  893. did anyone captured all the links shared in the chat
  894.  
  895. s p
  896. pls share if..
  897.  
  898. A W
  899. Can the group chat be added to the pastebin at the end of class?
  900.  
  901. J K
  902. So trap is almost like an interrupt handler in user space.
  903.  
  904. J K
  905. ?
  906.  
  907. J K
  908. Where is OPTIND declared?
  909.  
  910. S C
  911. its a builtin I think
  912.  
  913. B B
  914. Its an implicit variable no need to declare
  915.  
  916. B B
  917. how can you use the long option ?
  918.  
  919. A J
  920. backtick - not apostrophe
  921.  
  922. K E
  923. slanting tick should be used
  924.  
  925. . h
  926. the two apostrophes on the line above are unicode not ascii
  927.  
  928. K E
  929. instead of single quote
  930.  
  931. . h
  932. assai
  933.  
  934. A D
  935. plz user `
  936.  
  937. P C
  938. those are backticks
  939.  
  940. J V
  941. another quote in the bottom?
  942.  
  943. C M
  944. https://stackoverflow.com/questions/402377/using-getopts-to-process-long-and-short-command-line-options
  945.  
  946. C M
  947. if you want to have a look at long options
  948.  
  949. C M
  950. you have to do it "manually"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement