You are not logged in.

#161 2020-11-02 04:02:34

Sector11
Mod Squid Tpyo Knig
From: Upstairs
Registered: 2015-08-20
Posts: 8,011

Re: Handy command-line stuff for terminals or scripts

sleekmason wrote:

Here is the toggle solution:

#!/bin/bash
if pgrep -f ld-hotcorners > /dev/null; then
    pkill -9 -f ld-hotcorners
else
    ld-hotcorners --daemon &
fi

Fairly straightforward I suppose. Don't know why the one liner wouldn't work.
@ohnonot, the bash guide is good. Thanks:)

Which is very close to this.

Gotta love happy endings.


Debian 12 Beardog, SoxDog and still a Conky 1.9er

Offline

#162 2020-11-02 06:53:30

ohnonot
...again
Registered: 2015-09-29
Posts: 5,592

Re: Handy command-line stuff for terminals or scripts

Thou shalt not (p)kill -9.

Offline

#163 2020-11-02 13:19:17

sleekmason
zoom
Registered: 2018-05-22
Posts: 1,103
Website

Re: Handy command-line stuff for terminals or scripts

@Sector11  Yes! very close:)

@ohnonot Fixed and done!  Lol.  Wasn't about to touch it after I got it working. I had grabbed:

pkill -9 -f ld-hotcorners

from the ld-hotcorners script itself.  What does the -9 do?  man pkill doesn't seem to have that as an option.  Process #?

Last edited by sleekmason (2020-11-03 01:29:32)

Offline

#164 2020-11-02 14:21:32

damo
....moderator....
Registered: 2015-08-20
Posts: 6,734

Re: Handy command-line stuff for terminals or scripts

sleekmason wrote:

What does the -9 do?  man pkill doesn't seem to have that as an option.  Process #?

SIGKILL

man kill

A full list of SIG commands can be seen if you run htop, and use F9.


Be Excellent to Each Other...
The Bunsenlabs Lithium Desktop » Here
FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#165 2020-11-02 15:51:53

Sector11
Mod Squid Tpyo Knig
From: Upstairs
Registered: 2015-08-20
Posts: 8,011

Re: Handy command-line stuff for terminals or scripts

Ot just list in the kill command:

 02 Nov 20 @ 12:48:46 ~
   $ kill -l
 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 6) SIGABRT	 7) SIGBUS	 8) SIGFPE	 9) SIGKILL	10) SIGUSR1
11) SIGSEGV	12) SIGUSR2	13) SIGPIPE	14) SIGALRM	15) SIGTERM
16) SIGSTKFLT	17) SIGCHLD	18) SIGCONT	19) SIGSTOP	20) SIGTSTP
21) SIGTTIN	22) SIGTTOU	23) SIGURG	24) SIGXCPU	25) SIGXFSZ
26) SIGVTALRM	27) SIGPROF	28) SIGWINCH	29) SIGIO	30) SIGPWR
31) SIGSYS	34) SIGRTMIN	35) SIGRTMIN+1	36) SIGRTMIN+2	37) SIGRTMIN+3
38) SIGRTMIN+4	39) SIGRTMIN+5	40) SIGRTMIN+6	41) SIGRTMIN+7	42) SIGRTMIN+8
43) SIGRTMIN+9	44) SIGRTMIN+10	45) SIGRTMIN+11	46) SIGRTMIN+12	47) SIGRTMIN+13
48) SIGRTMIN+14	49) SIGRTMIN+15	50) SIGRTMAX-14	51) SIGRTMAX-13	52) SIGRTMAX-12
53) SIGRTMAX-11	54) SIGRTMAX-10	55) SIGRTMAX-9	56) SIGRTMAX-8	57) SIGRTMAX-7
58) SIGRTMAX-6	59) SIGRTMAX-5	60) SIGRTMAX-4	61) SIGRTMAX-3	62) SIGRTMAX-2
63) SIGRTMAX-1	64) SIGRTMAX	
 
 02 Nov 20 @ 12:50:39 ~
   $ 

More reading: Why should I not use 'kill -9' / SIGKILL


Debian 12 Beardog, SoxDog and still a Conky 1.9er

Offline

#166 2020-11-02 17:36:06

sleekmason
zoom
Registered: 2018-05-22
Posts: 1,103
Website

Re: Handy command-line stuff for terminals or scripts

Wow!  Have not seen all of those before now. Thank you both for the information:)

From the link:

Under the covers even trivial-seeming programs do all sorts of transactional work that they need to clean up from before terminating (think of the finally block in Java and other programming languages), such as closing resource handles, deleting temporary files, and flushing data from memory to disk. You cannot anticipate when a program might be doing something like this.

If you're lucky you won't notice anything amiss if you send a SIGKILL, but you can't be lucky forever, and you may not know anything's gone wrong until it's too late to recover what's been lost.

That sums it up nicely.

Offline

#167 2020-11-02 20:47:11

Sector11
Mod Squid Tpyo Knig
From: Upstairs
Registered: 2015-08-20
Posts: 8,011

Re: Handy command-line stuff for terminals or scripts

Notice my little script does NOT use "pkill -9".

If running shut it down
If not running, start it up  smile


Debian 12 Beardog, SoxDog and still a Conky 1.9er

Offline

#168 2020-11-03 01:33:15

sleekmason
zoom
Registered: 2018-05-22
Posts: 1,103
Website

Re: Handy command-line stuff for terminals or scripts

*Ouch! Just saw where I may have inferred such inadvertently. Changed to reflect the ld-hotcorners script;)

Offline

#169 2020-11-03 07:03:46

vasa1
Member
Registered: 2015-09-29
Posts: 204

Re: Handy command-line stuff for terminals or scripts

Sector11 wrote:

...
I also have a SSC.sh file to Start|Stop Conky, various ones, that might help you, adapt it for your need:

#!/bin/bash
## Original idea by: GrouchyGaijin
## This idea by: Stinkeye - Jan 2013
## With another tweak by: arclance
## Final tweak by: Sector11
## click to start, click to stop

if pgrep -f "conky -q -c /media/5/Conky/easysid/MechClock/conkyrc_clock"
then
	pkill -xf "conky -q -c /media/5/Conky/easysid/MechClock/conkyrc_clock"
else
	conky -q -c "/media/5/Conky/easysid/MechClock/conkyrc_clock"
fi

It checks to see if the conky is running:
if yes - it kills it
if no - it starts it

Worth a try. 
Remember the part inside the " " are the exact command to run the program.

Looks like the "pgrep" bit can be left out:

#!/bin/bash

pkill -xf "conky -c /path/to/conky" || conky -c "/path/to/conky"

Using the Openbox (3.5.2) session of Lubuntu 14.04 LTS but very interested in BL :)

Offline

#170 2020-11-06 22:08:11

ohnonot
...again
Registered: 2015-09-29
Posts: 5,592

Re: Handy command-line stuff for terminals or scripts

sleekmason wrote:

What does the -9 do?

http://turnoff.us/image/en/dont-sigkill.png

Offline

#171 2020-11-07 02:27:50

sleekmason
zoom
Registered: 2018-05-22
Posts: 1,103
Website

Re: Handy command-line stuff for terminals or scripts

ohnonot wrote:
sleekmason wrote:

What does the -9 do?

http://turnoff.us/image/en/dont-sigkill.png

Ha!  This is good:)

Offline

#172 2020-12-11 18:30:27

sleekmason
zoom
Registered: 2018-05-22
Posts: 1,103
Website

Re: Handy command-line stuff for terminals or scripts

Made this bit to find which window manager to display in Conky.

wmctrl -m | head -n1 | cut -d" " -f2

Offline

#173 2020-12-11 20:37:05

rbh
Moderator
From: South of Lapplands inland
Registered: 2016-08-11
Posts: 1,921

Re: Handy command-line stuff for terminals or scripts

How do you manage the need to run as root or with sudo, when in conky?


// Regards rbh

Please read before requesting help: "Guide to getting help", "Introduction to the Bunsenlabs Lithium Desktop" and other help topics under "Help & Resources" on the BunsenLabs menu

Offline

#174 2020-12-11 21:17:02

ohnonot
...again
Registered: 2015-09-29
Posts: 5,592

Re: Handy command-line stuff for terminals or scripts

rbh wrote:

How do you manage the need to run as root or with sudo, when in conky?

It comes down to being able to execute commands with superuser privileges without the need of entering a password (interactively).
Several approaches exist: with PAM I guess, or with adding certain commands to your sudoers config files under /etc.
For the latter, see: https://wiki.archlinux.org/index.php/Su … le_entries

Offline

#175 2021-02-15 07:18:53

ohnonot
...again
Registered: 2015-09-29
Posts: 5,592

Re: Handy command-line stuff for terminals or scripts

I was playing around with using conky to display a visual notification from a shell script.
Conky can read config files from stdin! That opens a lot of possibilities...
Proof of concept: find your mouse cursor - displays a red square around your pointer for 2 seconds. Requires xdotool.

#!/bin/sh

showtime=2 # seconds
eval $(xdotool getmouselocation --shell)

cat <<EOF | conky -c - & conkypid=$!
conky.config = {
own_window = true,
own_window_transparent = false,
own_window_colour = '#ff0000',
own_window_hints = 'undecorated,skip_taskbar,skip_pager,sticky',
update_interval = 1,
double_buffer = true,
minimum_width = 20,
minimum_height = 20,
maximum_width = 20,
alignment = 'top_left',
gap_x = $X,
gap_y = $Y,
draw_borders = true,
draw_outline = false,
draw_shades = false,
border_width = 4,
border_outer_margin = 2,
default_color = '#ffffff',
};
conky.text = [[]];
EOF

# total_run_times is unreliable, therefore:
sleep "$showtime"
kill $conkypid

Offline

#176 2021-02-15 09:18:36

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 12,560
Website

Re: Handy command-line stuff for terminals or scripts

ohnonot wrote:

I was playing around with using conky to display a visual notification from a shell script.
Conky can read config files from stdin! That opens a lot of possibilities...

Nice find!

---
Other topic:
sh -c '...some code...'
is sometimes useful when you want to run a snippet of shell code from a menu item, Thunar custom action or tint2 or any other situation that doesn't support shell syntax. sh (or bash if you need the extra features) is the command to run, and everything after the -c is just a parameter, so the launcher doesn't have to interpret it at all, just send it to the shell. It's highly advisable to wrap that shell code in single quotes so it won't be touched by anything else.

But this is already well known. Possibly slightly less well known is that you can pass positional parameters to that code snippet, just as if it was a function or separate script in a file.

Try this in a terminal:

sh -c 'echo "First: $1, Second: $2"' _ one two

Just a minute, what's that _ doing? That gets passed as $0, which usually means the shell name or script name. Here it's a placeholder (still accessible as $0) that gets used in error messages:

sh -c  'ecko "First: $1, Second: $2"' mycode one two
# returns:
mycode: 1: mycode: ecko: not found

So sometimes there is value in using a meaningful string in the zeroth place, but usually _ is enough.

More reading:
https://superuser.com/questions/1526229 … ll-code-sh

Last edited by johnraff (2021-03-11 09:00:15)


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )

Introduction to the Bunsenlabs Boron Desktop

Offline

#177 2021-03-11 09:35:39

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 12,560
Website

Re: Handy command-line stuff for terminals or scripts

queue multiple instances of a script with 'flock'

Flock (comes with util-linux, which you likely have) is used for reliable locking, instead of unatomic lockfiles.

But this use case is something that had been bothering me for a while, and finally websearched up a fix. I'm going to be specific here, because the code is simple but maybe not so easy to understand.

I like audio feedback from scripts sometimes, because I find it less distracting when I'm focussing on something quite different on the screen. Little messages like "backing up somefile" or "yourscript has returned an error!" I use a script called 'say', a wrapper round espeak with tweaked settings. What was annoying me was my shutdown script, which closes dropbox and goes on to do a simple backup. Snippet:

( dropbox stop | say ) &
say "About to do backup mirror to hard disk." &
"$HOME"/scripts/backup-ssd-rsync.sh &

The 'say' commands are forked into the background so that the script doesn't have to wait till it's finished speaking before it goes on to the next operation. Trouble is that if two 'say' messages are triggered one after the other they will overlap and be unintelligable. I want the 'say' script to wait till any previously running instances have finished before speaking its message.

It sounds complicated, but all you need is this code at the top of 'say' (needs bash though):

lock="/tmp/say.lock"
exec {fd}>$lock
flock --timeout 60 "$fd" || exit 1

That will work for any script if you want it to queue up - just choose a differently named lock file for other scripts. You might want a much longer timeout then 60s for scripts that run a long time of course - if the timeout expires the script will give up waiting in the queue.

I could imagine people wanting to queue up media file conversions, for example, might find this useful. cool

The code is simple, but uses redirection and a special bash way of allocating the next available file descriptor - see these pages to read about it, and maybe get some more ideas:
https://blog.skbali.com/2019/03/queue-u … ll-script/
https://stackoverflow.com/a/17030546
https://jdimpson.livejournal.com/5685.html
And 'man flock' of course.

Here's my full 'say' script (the aplay pipe is probably no longer needed):

#!/bin/bash
#say.sh (customized espeak)

# https://blog.skbali.com/2019/03/queue-up-multiple-instances-of-a-shell-script/
# https://stackoverflow.com/a/17030546
# https://jdimpson.livejournal.com/5685.html

lock="/tmp/say.lock"
exec {fd}>$lock
flock --timeout 60 "$fd" || exit 1

hash espeak || {
    echo "$0: needs espeak" >&2
    exit 1
}

if [[ -z $1 ]]
then
    espeak -k20 -s150 --stdout | aplay > /dev/null 2>&1
else
    espeak -k20 -s150 "$1" --stdout | aplay > /dev/null 2>&1
fi

exit 0

Last edited by johnraff (2021-03-12 02:14:26)


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )

Introduction to the Bunsenlabs Boron Desktop

Offline

#178 2021-03-11 11:00:05

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,741

Re: Handy command-line stuff for terminals or scripts

Neat^, bookmarked.

p.s. I don't get what is {fd} and why it is even needed (but I'll read on when implementing something like that).

Online

#179 2021-03-11 14:04:10

sleekmason
zoom
Registered: 2018-05-22
Posts: 1,103
Website

Re: Handy command-line stuff for terminals or scripts

+1 Bookmarked.  This looks like fun:)

Offline

#180 2021-03-12 02:12:16

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 12,560
Website

Re: Handy command-line stuff for terminals or scripts

brontosaurusrex wrote:

I don't get what is {fd} and why it is even needed

Man bash > /^redirection > 2nd paragraph

Tricky stuff.


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )

Introduction to the Bunsenlabs Boron Desktop

Offline

Board footer

Powered by FluxBB