Hello. I’m trying to create a simple restart script that checks the value of an environment variable on a RedHat system. What I have is below, but the if statement is failing with a message of “Relevance substitution error”
Anyone have any suggestions to fix this?
if {value of variable "SSH_CONNECTION" of environment = ""}
restart 10
else
exit 1
endif
I suspect that the SSH_CONNECTION environemnt variable is only defined when you are working in a ssh session, so it is not defined when you run it within the actionscript. It may eventually be OK in QNA (because you run it from a ssh session, right?), but you shoud error trap the statement in case the environment varialble doesn’t exist.
A statement that may catch it is (concatenation of values of variables "SSH_CONNECTION" of environment) = ""
In any case beware if you use “SSH_CONNECTION” environment variable in an action script: you will aways run the true branch (the environment variable is not defined) and the restart will always happen!
p.s: for your information, the available variables available in an actioscript in my environment are:
Every process has its own environment, its own set of environment variables. The BESClient process environment does not have the SSH_CONNECTION variable, just like userA would not have the variable if userB is logged on via ssh.
Thank you for that information. I came to that conclusion also as I was troubleshooting. I pivoted to this below and it’s working so far.
// Enter your action script here
if {exists file "/tmp/who.txt"}
delete "/tmp/who.txt"
endif
// Run the shell command to list active SSH connections into a text file
wait /bin/sh -c "who > /tmp/who.txt"
// Check if the text file is empty and take action
if {not exists lines of file "/tmp/who.txt"}
// Ensure the text file is deleted
delete "/tmp/who.txt"
// Restart the computer if the file is empty
restart 10
else
// Exit with failure if the file is not empty
exit 1
endif
Nathan,
if your objective it to reboot the system when there are no users logged on I think you may use some better “BigFix” approach, that doesn’t require to create a file and parse it, like using the “logged on users” inspector in the fixlet relevance, rather than inside the actionscript. So the action can do the restart of the system, but agent will only start its execution when the number of logged on users < 1.
I agree with using a cleaning BigFix method, but it just wouldn’t work. I initially tried this below, but it did not work, even when I validated the results of the if statement, the system would restart if if there was an active SSH session. The goal was mainly check for active remote sessions. I wasn’t worried about any console session at the time of restart. I didn’t try to bake it into the fixlet relevance though. I’ll test it out.
if {not exists logged on user}
restart 10
endif
Results of query when I was ssh’d into host I was trying to not have restart, but it would.
I don’t want the machine to restart if there are active SSH session. I’m not worried about local session at 3am because the room is locked, but the system is accessible by SSH 24 hours a day.
As Kapax suggest, I just baked this relevance into the task, and it still restarted the system while I was ssh’d into the host.
I wonder if there may be an error because of ‘singular/plural’ evaluation results: did you try with “logged on users” vs “logged on user”?
When more than one exist you could get a ‘singular’ exception and it could invalidate your evaluation.
@_nathan,
for a BigFix style task, I tested the relevance that can be used to detect the existence of one of more users remotely accessing via ssh the environment:
exist logged on users whose (name of process (process id of it) is "sshd")
By creating a property named like “SSHSessions” with the following value:
number of logged on users whose (name of process (process id of it) is "sshd")
you can condition your task/action to execute only when
“SSHSessions” < “1”.
I tested this, and while I have the reboot task submitted (and relevant) it will not start the reboot if I have remote ssh sessions to the system. (action status goes into
“constrained”)
As soon as I logoff from the ssh session… reboot happens.
hope this helps
Andrea