Becoming /dev

Started by Dieselboy, September 18, 2019, 09:37:26 PM

Previous topic - Next topic

Dieselboy

I had a problem with setting up a VNC server on Ubuntu. A previous attempt by another person left around 100 processes running on the server, which was visible by ps -ef | grep vnc

So I needed to kill all these processes but was not going to sit there and manually do each one. So the only way I knew how was to:
1. copy the ps -ef output to text file
2. open Microsoft Excel and import the text file as a CSV data source
3. delete the unneeded cells so we're left with the PID of each process and save again as CSV
4. open the CSV as text in notepad++ and use find/replace to make the running user (ubuntu) into "kill -9"
5. now we have a file with around 100 lines of "kill -9 xxxx" where xxxx is the PID
6. paste this into the SSH shell to kill all the processes.

Genius! I thought. I explained to my dev team and they replied with:

Quote👍
ps -ef | grep vnc| awk {'print $2'} | xargs kill -9

deanwebb

I can grep, but I can't awk. Or xargs. OK, I can't really grep, either...
Take a baseball bat and trash all the routers, shout out "IT'S A NETWORK PROBLEM NOW, SUCKERS!" and then peel out of the parking lot in your Ferrari.
"The world could perish if people only worked on things that were easy to handle." -- Vladimir Savchenko
Вопросы есть? Вопросов нет! | BCEB: Belkin Certified Expert Baffler | "Plan B is Plan A with an element of panic." -- John Clarke
Accounting is architecture, remember that!
Air gaps are high-latency Internet connections.

Otanx

grep, awk, sed, etc arn't the hard part. It is the regex. We used to have a dev that said "If you have a problem, and think regex is a solution you now have two problems".

-Otanx

Dieselboy

#3
I struggled to understand AWK. I have a script that is called during a kickstart script. Basically you boot the system from USB installation drive (centos 7) and the script needs to work out where the automated OS install is installed to. Basically, not any usb HDD, so filter them out and the remaining drive is the laptop SSD. So install there. It looks like this:

USB=$(ls -l /dev/disk/by-id/ | grep usb | grep -v part | awk '{ print $NF }' | sed -r 's/..\/..\///g' |  sed 'N;s/\n/,/')

The awk part... was difficult to understand.

The sed part is regex and easier :D I've been using regex for phone number translations and now I'm using it for build scripts. Like this:

Set an account called `admin` to be a system account, so that it does not show up in the login console as a user:
sed -i 's/^\(SystemAccount\=\)false/\1true/' /var/lib/AccountsService/users/admin

Then I wanted to make the `wheel` group allow sudo without being prompted for a password:
sed -i 's/^#\s*\(%wheel\s\+ALL=(ALL)\s\+NOPASSWD:\s\+ALL\)/\1/' /etc/sudoers
sed -i 's/^\(%wheel\s\+ALL=(ALL)\s\+ALL\)/#\1/' /etc/sudoers


The above is run twice because there are two wheel lines of config in the default sudoers file. The first line basically just removes the comment. The 2nd gets a comment added.

And then I wanted to make the grub menu disappear when booting up:
sed -i 's/GRUB_TIMEOUT=5/GRUB_TIMEOUT=0/g' /etc/default/grub

I was quite pleased with the above but may be it's relatively simple in the larger context. I would be keen to have a go at more complex stuff. I have tried to use the online regex helper sites but they are very confusing. I have not had any success with any of them. I have had success by checking the man pages or looking online for examples. The examples helped because then I check the man pages for what the specific switches do in the command and figure it out that way.