restart bash script while loop by itself using signal?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
restart bash script while loop by itself using signal?
I am trying to get a bash script while loop restart itself using signal properly.
I wrote testing.sh like this below
Code:
#!/bin/bash
function restart {
$0 "$@"
exit 0
}
trap restart SIGINT
num=0
while true; do
echo $num
((num=num+1))
sleep 1s
done
if I run that testing.sh in terminal, as you know it will show incremental number with sleep 1s interval. And if I hit Ctrl+c, the script will restart itself showing the number starting from 0.
Everything seems fine but
Code:
$ pgrep -f testing.sh
shows many PIDs.
Is that a problem?
Because I intended to use it for swaybar status so I can update it by hitting volume control keys, etc.
The way you have it written, the signal causes the script to spawn another process. So each time you interrupt it, there will be yet another script running. If all you wish to do is to reset the counter variable, then have the function do that, and only that:
Code:
#!/bin/bash
function restart {
num=0
}
trap restart SIGINT
num=0
while true; do
echo $num
((num=num+1))
sleep 1s
done
The way you have it written, the signal causes the script to spawn another process.
What I wish to do is to kill itself and start again itself from the beginning.
Showing incremental numbers is just for testing, I will replace it with other command for the status of sway window manager.
What I wish to do is to kill itself and start again itself from the beginning.
Showing incremental numbers is just for testing, I will replace it with other command for the status of sway window manager.
This looks similar how daemon processes work (= daemon reload/restart), but usually that means you save the pid of the process and later kill and restart it from outside. Do not reinvent the wheel.
super + Up
amixer sset Master 5%+; kill -SIGTERM $(pgrep dwm-status.sh)
super + Down
amixer sset Master 5%-; kill -SIGTERM $(pgrep dwm-status.sh)
super + Right
amixer sset Master toggle; kill -SIGTERM $(pgrep dwm-status.sh)
The important part is to background the 'sleep' command then have the status script wait and if the volume is changed to kill the 'sleep' process so the status will be updated in real time.
My bash scripting isn't the greatest so there's probably room for improvement.
The important part is to background the 'sleep' command then have the status script wait and if the volume is changed to kill the 'sleep' process so the status will be updated in real time.
So kill just errors out with a usage message because $(jobs -p) doesn't return a pid (since there's no background process). Otherwise the script functions as it should.
So kill just errors out with a usage message because $(jobs -p) doesn't return a pid (since there's no background process). Otherwise the script functions as it should.
or it will kill a df, awk or other processes.
by the way, you may also try the pkill command:
Code:
kill -SIGTERM $(pgrep dwm-status.sh)
# replace to
pkill -SIGTERM dwm-status.sh
or it will kill a df, awk or other processes.
by the way, you may also try the pkill command:
Code:
kill -SIGTERM $(pgrep dwm-status.sh)
# replace to
pkill -SIGTERM dwm-status.sh
I don't think I'm getting what you're saying.
Code:
kill -SIGTERM $(pgrep dwm-status.sh)
Sends a sigterm to dwm-status.sh. Which in turn traps that signal and then update() sends a sigterm to any background process launched and in this case, if there is one (might not be sleeping) would be sleep.
I'm unclear as to why this might kill df, awk, or other processes since I'm only killing a bg process and the only one would be sleep.
Because I intended to use it for swaybar status so I can update it by hitting volume control keys, etc.
It seems you should elaborate on this original problem. Sway support key bindings and swaybar appears to have stop/continue behaviour already - so what exactly is it you want to do that needs to be different to the way everyone else is doing it?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.