You are not logged in.

#1 2018-09-06 01:14:08

Kirill Bugaev
Member
Registered: 2018-08-03
Posts: 173

[SOLVED] Name independent toggle

I write a bash script for toggle conky

#!/bin/bash

if ! pgrep conky >/dev/null; then
    echo "Turning conky ON"
    conky &
else
    echo "Turning conky OFF"
    pkill conky
fi

exit 0

If I name this script conky_toggle.sh and run then "pgrep conky" will always return 0 like conky already run.
How to write this kind of scripts correct? What can replace pgrep and pkill?

Last edited by Kirill Bugaev (2018-09-06 02:09:14)

Offline

#2 2018-09-06 02:08:33

Kirill Bugaev
Member
Registered: 2018-08-03
Posts: 173

Re: [SOLVED] Name independent toggle

pgerp -x, pkill -x.
Or write C daemon program which will save process id of runned conky and toggle.

Offline

#3 2018-09-06 02:40:15

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,238
Website

Re: [SOLVED] Name independent toggle

Instead of pgrep, use pidof with a full path:

pidof /usr/bin/conky

Offline

#4 2018-09-06 03:08:40

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,541
Website

Re: [SOLVED] Name independent toggle

Or busybox's version doesn't need a full path but matches the full name:

/usr/lib/initcpio/busybox pidof conky

This is particularly useful for python programs (e.g., qutebrowser) as coreutils' pidof will not match their names or full path at all.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#5 2018-09-06 04:48:26

Kirill Bugaev
Member
Registered: 2018-08-03
Posts: 173

Re: [SOLVED] Name independent toggle

Thanks a lot. Will use busybox version.

Last edited by Kirill Bugaev (2018-09-06 04:49:06)

Offline

Board footer

Powered by FluxBB