Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
In the web I have found a script called "Autosuspend".
I'm no programmer. But it is my understanding that the part of the script below looks for active clients on port 9981 and 9982.
Though I have one or more active clients on port 9982 the variable "$active_clients" never gets TRUE.
Can someone tell me if there is a mistake in this part of the script.
The script is running on Linux Mint 20.
Here is the part of the script that is not working
TVHEADEND_PORTS="$TVHEADEND_HTTP_PORT $TVHEADEND_HTSP_PORT"
LANG=C
active_clients=()
for port in $TVHEADEND_PORTS; do
IFS=$'\n' active_clients+=($(netstat -n | grep -oP "$TVHEADEND_IP:$port\s+\K([^\s]+)(?=:\d+\s+ESTABLISHED)"))
done
if [ $active_clients ]; then
logit "Tvheadend has active clients: ${active_clients[@]}"
return 1
fi
looks like the same problem as your other post. What I don't really understand: how do you want to solve it if you are not a programmer. [How] Do you think without any knowledge a script named autosuspend will do what you need? How do you know that part of the code which is not working?
You can debug your code with set -xv.
hostname -I may return more than one IP addresses, in that case the script cannot work.
By the way, would be nice to post your code in code tags.
@ pan64
When I was younger I programmed a little bit (Basic, Turbo Pascal, C++).
In principle I understand in which way the script "autosuspend" is working. I already checked some parts of the script. I checked e.g. that the "FOR DO" loop is running two times. And I understand that my pc is not running down when "$active_clients" is TRUE. But " $active_clients" never gets TRUE. That's why I try do understand this line
IFS=$'\n' active_clients+=($(netstat -n | grep -oP "$TVHEADEND_IP:$port\s+\K([^\s]+)(?=:\d+\s+ESTABLISHED)"))
Can you check if there is a mistake in this line?
I understand that this part of the script should check if there is traffic on port 9981 and 9982 of the localhost. If there is traffic "$active_clients" gets TRUE.
In the web I have found a script called "Autosuspend".
...
Here is the part of the script that is not working
Assuming the script came from someone or some site. Is there any way you can contact the author? They'd be the ideal person to ask about what they wrote.
As suggested you should post more, if not the whole script.
Also as said before you can use "set -xv" to enable debug and the part where you wrote "Here is the part of the script that is not working", what you should do is provide any output that you see indicating why that part doesn't work. Debug output would be helpful.
While we get that the diagnosis of this is above your current talents, people need more details as opposed to the clip provided.
I think the prefix
IFS=$'\n'
has no effect, can be omitted.
The prefix sets an environment for a forked shell, such as a command.
But an assignment = or += is not a command; no fork happens.
The environment is only set for the $( ) sub shell where it has no effect, neither in netstat nor in grep.
TVHEADEND_IP=$(echo ${TVHEADEND_IP:-$(hostname -I)} | tr -d [:space:])
should be
[ "$TVHEADEND_IP" ] || read TVHEADEND_IP junk < <(hostname -i)
or
[ "$TVHEADEND_IP" ] || TVHEADEND_IP=( $(hostname -i) )
Thank you for all this hints here. But I still don't understand how to change this line now.
IFS=$'\n' active_clients+=($(netstat -n | grep -oP "$TVHEADEND_IP:$port\s+\K([^\s]+)(?=:\d+\s+ESTABLISHED)"))
I tested again a little bit.
When I have NO active client and write in the terminal this
netstat -n | grep -oP 192.168.178.60:9982
I get as result "nothing".
When I have AN active client and write in the terminal this
netstat -n | grep -oP 192.168.178.60:9982
I get this line as result
192.168.178.60:9982
So my script needs to do something like this:
active_clients=()
if [ (netstat -n | grep -oP 192.168.178.60:9982) = (192.168.178.60:9982) ]; then
logit "Tvheadend has active clients: ${active_clients[@]}"
return 1
fi
It is my understanding that "return 1" means that "active_clients=1" right?
Can this work? What do I have to change in my script?
In the web I have found a script called "Autosuspend".
I'm no programmer. But it is my understanding that the part of the script below looks for active clients on port 9981 and 9982.
Though I have one or more active clients on port 9982 the variable "$active_clients" never gets TRUE.
Can someone tell me if there is a mistake in this part of the script.
The script is running on Linux Mint 20.
Here is the part of the script that is not working
TVHEADEND_PORTS="$TVHEADEND_HTTP_PORT $TVHEADEND_HTSP_PORT"
LANG=C
active_clients=()
for port in $TVHEADEND_PORTS; do
IFS=$'\n' active_clients+=($(netstat -n | grep -oP "$TVHEADEND_IP:$port\s+\K([^\s]+)(?=:\d+\s+ESTABLISHED)"))
done
if [ $active_clients ]; then
logit "Tvheadend has active clients: ${active_clients[@]}"
return 1
fi
could you be so kind as to click the advance and then click code then insert it in the code brackets Thank you
TVHEADEND_IP=$(echo ${TVHEADEND_IP:-$(hostname -I)} | tr -d [:space:])
TVHEADEND_HTTP_PORT=${TVHEADEND_HTTP_PORT:-9981}
TVHEADEND_HTSP_PORT=${TVHEADEND_HTSP_PORT:-9982}
TVHEADEND_PORTS="$TVHEADEND_HTTP_PORT $TVHEADEND_HTSP_PORT"
LANG=C
active_clients=()
for port in $TVHEADEND_PORTS; do
IFS=$'\n' active_clients+=($(netstat -n | grep -oP "$TVHEADEND_IP:$port\s+\K([^\s]+)(?=:\d+\s+ESTABLISHED)"))
done
if [ $active_clients ]; then
logit "Tvheadend has active clients: ${active_clients[@]}"
return 1
fi
On my Mint 20 VM since there is a line in /etc/hosts i.e. 127.0.1.1 <hostname> and there are no other virtual ports besides lo, hostname -I is the correct syntax. However, since I don't know exactly how your system is configured make sure the output is as expected. Run the command from a terminal.
hostname -I
There are a few statements that could be rewritten but the partial script "as is" works on my Mint 20 VM. I used ssh port 22 as a test instead of 9981,9982. I assume logit is a function and I did not need the return statement.
Code:
if [ $active_clients ]; then
I believe since $active_clients is an array the if statement checks if there an element 0. If there is the statement is true.
Thank you for all this hints here. But I still don't understand how to change this line now.
IFS=$'\n' active_clients+=($(netstat -n | grep -oP "$TVHEADEND_IP:$port\s+\K([^\s]+)(?=:\d+\s+ESTABLISHED)"))
I tested again a little bit.
When I have NO active client and write in the terminal this
netstat -n | grep -oP 192.168.178.60:9982
I get as result "nothing".
When I have AN active client and write in the terminal this
netstat -n | grep -oP 192.168.178.60:9982
I get this line as result
192.168.178.60:9982
So my script needs to do something like this:
active_clients=()
if [ (netstat -n | grep -oP 192.168.178.60:9982) = (192.168.178.60:9982) ]; then
logit "Tvheadend has active clients: ${active_clients[@]}"
return 1
fi
It is my understanding that "return 1" means that "active_clients=1" right?
Can this work? What do I have to change in my script?
I hoped that it is an easy and good idea what I have written here. No one an idea how this part of the script should look like?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.