Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### script 1 improvements
- [root@linfun2 cool-bash]# cat 1_cpu-hog
- #!/bin/bash
- # Script that monitors the top-active process. The script sends an email to the user root if
- # utilization of the top active process goed beyond 80%. Of course, this script can be tuned to
- # do anything else in such a case.
- #
- # Start the script, and it will run forever.
- while true
- do
- # Check every 60 seconds if we have a process causing high CPU load
- PUSAGE=$(ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1)
- USAGE1=$(echo $PUSAGE | awk '{ print $1 }')
- USAGE1=${USAGE1%.*}
- PID1=$(echo $PUSAGE | awk '{ print $2 }')
- PNAME1=$(echo $PUSAGE | awk '{ print $3 }')
- # Only if we have a high CPU load on one process, run a check within 7 seconds
- # In this check, we should monitor if the process is still that active
- # If that's the case, root gets a message
- if [ $USAGE1 -gt 80 ]
- then
- sleep 7
- PUSAGE2=$(ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1)
- USAGE2=$(echo $PUSAGE2 | awk '{ print $1 }')
- USAGE2=${USAGE2%.*}
- PID2=$(echo $PUSAGE2 | awk '{ print $2 }')
- PNAME2=$(echo $PUSAGE2 | awk '{ print $3 }')
- # Now we have variables with the old process information and with the
- # new information
- [ $USAGE2 -gt 80 ] && [ $PID1 = $PID2 ] && mail -s "CPU load of $PNAME is above 80%" root@blah.com < .
- fi
- sleep 6
- done
- ### AW long break solution
- root@linfun2 ~]# cat countdown2
- #!/bin/bash
- COUNTER=$1
- COUNTER=$(( COUNTER * 60 ))
- while true
- do
- echo $(($COUNTER/3600))' hours' $(($COUNTER%3600/60))' mins'$(($COUNTER%60))' secs remaining in break'
- COUNTER=$((COUNTER-1))
- sleep 1
- done
- #### SC solution to assignment 2
- [root@linfun2 cool-bash]# cat sc2
- #!/bin/bash
- set -u
- arg=$1
- test -n $arg || { echo "missing arg" 1>&2; exit 1; }
- if test -f $arg
- then : file
- vim $arg
- elif test -d $arg
- then : directory
- cd $arg
- exec bash
- else : other
- echo "unknown type" 1>&2
- exit 2
- fi
- ### GROUPCHAT
- Nurul I
- Hello everyone, welcome to the course Writing Cool Bash Shell Scripts in 3 Hours with Sander van Vugt. We hope you enjoy the course!
- Nurul I
- 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.
- Nurul I
- We will begin the course in approximately 14 minutes
- . h
- metaphorically twiddles thumbs while listening to Mozart
- Nurul I
- The course has started, you should all be hearing Sander now
- s p
- Hi All, Good Morning
- k k
- morning
- s p
- I d idnt get any mail about Replay of the Unix session had on 3rd Aug.. any thoughts pls
- D S
- Average experience
- A A
- Good morning all
- Nurul I
- sp, please email our customer service team for details, the team will be able to help- onlinetraining@oreilly.com. Thank you!
- P N
- Will it be possible to share earlier classes scripting examples?
- F S
- please show with production senario
- C L
- Hi, the "On which OS platform are you planning to use Bash shell scripts" question could perhaps be multiple-choice?
- P N
- We didn't had option to select both and Linux :-)
- E D
- @PN Bash Scripting 4 Hours repo should be publicly available at the Sander's github
- P N
- both Mac and Linux
- E D
- named bash-scripting
- F S
- online katakoda terminal
- J S
- katacoda is good
- P N
- Got it - https://github.com/sandervanvugt/bash-scripting
- J K
- git clone https://github.com/sandervanvugt/cool-bash.git
- sander v
- pastebin.com
- D S
- Yes
- P N
- @JK, I was referring to earlier class code of 4 hours... For present one is the one that you shared.
- T C
- @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.
- s p
- yes
- s p
- I didnt get any reminder mails ..
- s p
- Thanks for reply
- S C
- missing close back-tick on line 29
- g v
- hey guys whats the username for github?
- A W
- no username
- S C
- you don't need one
- g v
- ok
- J K
- install git
- J K
- git clone https://github.com/sandervanvugt/cool-bash.git
- g v
- thanks
- J K
- sleep 60 should be in the else block ?
- G S
- or download https://github.com/sandervanvugt/bash-scripting/archive/master.zip and then unzip if you don't have git
- E D
- Expected behavior is to run only once every 60 seconds.
- E D
- @JK
- L L
- There seems to be an error in the script. I just clone the repo, and I'm getting:
- J K
- Yeah I know
- L L
- $ 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
- J K
- But it then block has sleep 7
- J K
- *if then block
- J K
- If CPU usage > 80 then run every 7 seconds, else run every 60 seconds
- J K
- He asked what is one error in this code so I was trying to estimate
- A R
- weird apostrophes
- J K
- that executes the command
- G S
- Missing backtick
- E D
- Oh I see, of course it'll not loop in the if block. I agree with you
- c O
- instead of while true a cron job may be used?
- S C
- better an atjob than a cronjob
- L L
- There seems to be an error in line #29. missing ending backtick.
- M R
- 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.
- J K
- Agree with MR
- L L
- @MR, Is this possible with bash? Get $1, $2, $3 at the same time?
- S C
- @LL use eval
- K E
- PNAME2 should end with `
- L L
- I wonder if double square bracket [[ ]] would be a better choice over single square bracket [ ]?
- E D
- In terms of readability?
- s p
- Can we please get copy of the Group chat dump
- T Y
- Could probably run ps command once instead of 3 times to get usage, pid, pname
- S C
- would normally redirect /dev/null into mail command
- s p
- or keep in repository location
- H B
- ps should be used with --no-headers
- E D
- Group chat is being recored, you can view from recording later on
- S C
- I wouldn't want to be flooded with email every 60s !
- T C
- Agreed SC...that would be overkill!
- T I
- 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.
- N R
- in same script will give awk '{print $1 $2 $3}'
- M R
- the sleep 60 is too early
- B G
- 60 secs sleep?
- j m
- it's sleeping for 60 sec
- C M
- sleep 60 should be at the end maybe
- C M
- so the first run always happens first
- G S
- Agreed, ideally the 60 second sleep would be at the end of the loop.
- T C
- Yup TI...no descriptive variables makes it more challenging to debug someone else's code snippet.
- E D
- 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
- B G
- multivariable assignment
- J G
- use one line to gather the wanted values and separate their values from that
- S C
- set `ps -eo pcpu,pid -o comm=`
- B G
- but how is it done in bash?
- B B
- I get this: ./1_cpu-hog: line 20: [: 13,2: integer expression expected
- C M
- get all the fields in one awk
- N R
- will keep usage pid pname in single script
- G S
- Capture all the results at once so there's not mutability between execs
- S C
- USAGE=$1
- D S
- Capture process list results.
- B B
- capture usaage Pid name in awk command $1,$2, $3
- T Y
- get all 3 as a single string and then split them
- S C
- PID=$2
- P T
- captue results at once
- S C
- etc
- G S
- for the output of ps
- D S
- Pull values from the process list.
- D C
- break the utilisation logic into a function to make the script more readable
- C M
- +1 on that ^^
- D S
- I was going to mention that, isn't the new way $()
- j m
- is it more efficient?
- U 1
- what was the command run for creating a load of more than 80%
- J K
- $() will not be compatible with older Unix systems?
- U 1
- can anyone post
- j m
- @U1 dd if=/dev/zero of=/dev/null
- J K
- $@
- J K
- Sorry...
- J G
- USAGE1 not USAGE
- L L
- Is = for equality test bash specific? I thought the "standard" for string equality test is ==
- S C
- @LL it historically it used single =
- J K
- Why do a string comparison?
- S C
- [[ uses ==
- J K
- Can you use $PID -eq $PID2 ?
- L L
- @SC Thanks!
- V E
- set :nu
- M R
- you have PNAME
- S C
- or (( PID == PPID2 ))
- C M
- to get all 3 variables at once .. it should be something like this:
- M S
- vim +21 filename to open vim on a specific line, here in line number 21
- C M
- read var1 var2 <<< $(COMMAND | awk '{print $1,$2}')
- P T
- you don't need to declare variable PID1 and PNAME1 if usage1 is gt 80
- P T
- *if usage1 is not gt 80
- S C
- @cm nice
- N R
- else exit
- C M
- yes .. let's move on to next
- C M
- this is good
- H B
- cron will start new process every time. You will need additional logic for avoiding multiple processes
- sander v
- read var1 var2 <<< $(echo "$COMMAND | awk '{print $1,$2}')
- H B
- read -a foo <<<"$(ps -eo pcpu,pid -o comm= --no-headers| sort -k1 -n -r |head -1|sed 's/ /& /g')"
- H B
- if we want to use array
- T K
- pastebin link ?
- T Y
- 5 min or 12 min?
- A W
- he said 12min
- T Y
- @AW thanks. I thought I heard 5
- A W
- :q!
- A W
- wrong window
- s p
- 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 ..
- s p
- for review - gives better readability also
- J K
- 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
- J K
- I do have extensive C# and some Linux experience.
- J K
- I do not think that last script was too advanced, which is good or I would have been lost.
- A W
- @JK sounds like you have a excellent understanding of techniques and could pick up just about any language
- S C
- but this was supposed to be an advanced course
- J K
- Advanced is a relative term I guess, lol.
- S C
- :-)
- C M
- true
- E D
- @JK hehehe correct :)
- K E
- while true // what is it checking against?
- A R
- When does Sander give an hour long break?
- J K
- 'true' is the condition
- J K
- It will always be true and run forever
- T K
- :) \
- A R
- :D
- S C
- for seq in `seq 1 720`; do sleep 1; done
- A W
- have we restarted ? I can't hear anything - want to see if it is audio on my side
- C M
- yes we have
- A R
- We have
- S C
- he's speaking
- A W
- thanks
- A R
- f5 your browser
- s p
- refresh browser
- A W
- thanks
- S C
- should use +'%Y-%m-%d'
- S C
- ISO standard!
- S C
- lol
- T Y
- # - left to right and % - right to left?
- H F
- If you already have good grep is it worth learning this pattern matching?
- S C
- or use 'expr'
- T Y
- BLAH lol
- T K
- i should start using it instead of temp :p
- C T
- can we use "cut"
- S K
- -
- S K
- -F"-"
- D S
- $() you need
- F M
- replace "-" with space via sed | cut ? ugly but should work
- S C
- cut -d "-" -f2
- A D
- so finally which way is better , patren match,cut or awk ?
- J K
- pattern
- J K
- It uses only internal commands
- D S
- Please show the command again.
- A D
- ok thx
- S C
- IFS="-" read TODAY THISMONTH YEAR <<<$(date +%Y-%m-%d)
- T Y
- SC's version wins
- D S
- he has Year Month Date in the incorrect order
- S C
- true, soz
- D S
- =)
- J K
- first line is not shebang
- J K
- which script?
- T Y
- assignment in slides
- J K
- tx
- C M
- a script that those what is asked in assignement
- S S
- ^d
- J K
- what is :
- J K
- Is that for comment?
- B B
- what is : directory
- S C
- its a comment which shows up when run via "bash -x" it helps to debug
- J K
- why not exec before bash?
- J K
- Tx SC
- B B
- Thanks
- S C
- that was my -u sorry
- A W
- where do I see pastebin for this class?
- B G
- end of class
- A W
- Tx @BG
- J K
- Are keywords like 'if', 'else' considered external to shell? I think not perhaps
- S C
- builtin
- A W
- Here is good list of file test operators --> https://linuxize.com/post/bash-check-if-file-exists/
- U 1
- why was vi used here
- U 1
- vi $1
- B G
- the idea was that the argument should be passed either to vi or cd
- U 1
- ok
- L n
- SC can you share you script please?
- S C
- https://pastebin.com/7zHwqGk6
- L n
- Thank you ! :)
- J K
- I am not familiar with pastebin. How long is that link valid?
- G S
- @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"
- J K
- Thanks @SC
- s p
- @sc
- B G
- 1 month unless you have a pro subscription I think
- s p
- can u pls tell what 1>&2 does here
- s p
- &
- S C
- it redirects stdout to stderr
- s p
- thanks
- C M
- https://stackoverflow.com/questions/818255/in-the-shell-what-does-21-mean
- s p
- Thnx
- J K
- I have not seen that before. Why would you want to redirect stdout to stderr?
- F M
- 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
- F M
- ... config
- G S
- @JK if you want to pipe both stdout and stderr to a file or a pipe
- S C
- 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
- G S
- redirect to a file that is.
- F M
- and those are "n" values, meaning the number of values change from environment to another
- S C
- command1 |& command2
- S C
- will pass both stdout and stderr to command2
- S C
- otherwise stderr is not sent to it, just to the console
- s p
- @Sc - will there be error file - any specific location
- s p
- or same as in the current loc
- J K
- okay, thanks @GS and @SC
- s p
- Thanks @GS and @SC
- F M
- you can append a line to .bashrc that is run after reboot
- J K
- not, bash_profile ?
- F M
- then make sure you remove that line after execution
- S C
- dangerous
- F M
- i agree
- J K
- Oh, I get it. REBOOT is just a label, not an actual reboot since bash is case sensitive.
- S C
- no
- C M
- sara connor
- J K
- ha ha
- S C
- i am the terminator! ;-)
- C M
- ha ha
- J K
- He'll be back...
- J K
- In other classes
- T K
- :D
- S C
- (y)
- G S
- hasta la vista
- T K
- What was export DIR in previous script ?
- j m
- while true do dd if=/dev/zero of=/dev/null done
- T Y
- I think it would be more useful if it was a slow ramp up
- J K
- Very useful. We try to exhaust systems to see how our applications work with low resources.
- J K
- notepad++
- J K
- vim is the best.
- j m
- vim
- F M
- emacs
- A S
- in intelliJ you have a plugin
- B D
- VIM, or Visual Studio Code is a good free GUI
- . h
- nevoid with plugins
- B D
- also it's extremely useful to use shellcheck https://github.com/koalaman/shellcheck
- . h
- sorry NeoVim
- C M
- thanks a lot guys!!
- T Y
- It's a trap!
- . h
- NeoVim is Vim rewritten with a lot of the ancient cruft removed and is faster, more stable etc.
- s p
- did anyone captured all the links shared in the chat
- s p
- pls share if..
- A W
- Can the group chat be added to the pastebin at the end of class?
- J K
- So trap is almost like an interrupt handler in user space.
- J K
- ?
- J K
- Where is OPTIND declared?
- S C
- its a builtin I think
- B B
- Its an implicit variable no need to declare
- B B
- how can you use the long option ?
- A J
- backtick - not apostrophe
- K E
- slanting tick should be used
- . h
- the two apostrophes on the line above are unicode not ascii
- K E
- instead of single quote
- . h
- assai
- A D
- plz user `
- P C
- those are backticks
- J V
- another quote in the bottom?
- C M
- https://stackoverflow.com/questions/402377/using-getopts-to-process-long-and-short-command-line-options
- C M
- if you want to have a look at long options
- C M
- you have to do it "manually"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement