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.
Sample from my script. One section runs as root, other as user.
Code:
case "$SCRIPTCALL" in
mediastop)
if [[ "$1" ]] ; then
for seconds in 30 20 10 ; do
kodi-send --action \
"Notification(kodi ${1} in ${seconds} seconds,standby!)"
sleep 10
done
kodi-send --action "${1}"
fi
exit
;;
esac
else
case "$SCRIPTCALL" in
mediastop)
if [[ "$HOSTNAME" == megalith ]] && [[ "$USER" == jason ]] ; then
if [[ "$1" ]] ; then
for machine in gst main mbr ; do
if ping -c 1 kodi."$machine".mylan.home ; then
ssh root@kodi."$machine".mylan.home "mediastop ${1}" &
fi
done
fi
fi
;;
in the second mediastop to run as a non root user i run the ssh with an ampersand. After running this it rolls through my pxe booted kodi machines and does it's job. But it never exits properly. The terminal doesn't return to a prompt unless I do a Ctrl-C. Trivial maybe. Just doesn't feel finished unless I can correct that.
Not a make or break issue, just annoying. How can I force it to return to the prompt correctly when run from a terminal.
These are all pxe boot dumb terminals, so I'm not worried about using root on them. Read only filesystem with ssh keys only.
Last edited by jmgibson1981; 12-09-2019 at 11:35 AM.
I am trying to be able to remotely shutdown all my kodi pxe machines with an unprivileged user on the main server. These are read only root filesystems systems via LTSP. This is why I am not worried about root ssh access on them. I have ssh access to them all via ssh keys only. No passwords. I do not want to have to type in a password to do this, hence why I setup my user ssh > root @ the machines. Run script on server, it loops through all 3 machines in turn and runs the countdown timer, then shuts them off or reboots depending on which parameter i pass to $1.
The purpose of the & is so they fork and the script keeps working through the machines. 3 machines right now in turn would be a 1:30 or so runtime for all 3 shutdowns if I let ssh run it's course. I want them all simultaneously, or close to it within a few seconds. 30-35 seconds and done. I want it to be scalable. If I was to double my machine count I would be looking at a 3 minute or so runtime to shutdown all 6. Not what I want.
I suppose my ultimate goal here is to shut them all off at the same time at night, and wakeonlan at the same time the next day to be managed via my non privileged user on my main machine's crontab.
*EDIT* I did not fully understand the -f flag in the man page, however it had the same result. Script worked fine but did not exit to the prompt when finished.
Last edited by jmgibson1981; 12-09-2019 at 02:25 PM.
I suspect the issue is that the background jobs don't report back to the calling script that they've completed.
What happens if you run the calling script in the background:
Code:
scriptname mediastop &
You may have to add a log file to see...or add -x to the script.
Alternate idea: add an exit to the second case statement.
if ping -c 1 kodi."$machine".mylan.home ; then
ssh -f root@kodi."$machine".mylan.home "mediastop ${1}"
fi
or
Code:
if ping -c 1 kodi."$machine".mylan.home ; then
ssh -i /path/to/key_ed25519 -f root@kodi."$machine".mylan.home "mediastop ${1}"
fi
Note that the key does not have to be in the root directory even if it is connecting to root on the kodi machine. Nor does the SSH client need to run as root.
If that does not free up your script from hanging then you might try putting "set -xv" up near the top and following what it is really doing step by step.
Asking for clarification:
Doesn't the -f option cause the remote session to run in background?
I'm reading that the OP wants the local execution to be run in background.
Do I misunderstand?
Is there some other way to run the jobs in the loop concurrently?
The -f causes the SSH client to go into the background. It also prevents the client from reading stdin. So if the goal is to launch a series of remote scripts and drop through to the rest of the script without waiting, then that is one way to do it.
Note that the OP reported that -f didn’t solve their problem.
I guess we'd have to see more of the script then or wait back to hear what an audit using -x or -xv did. I'm unable to duplicate the problem as described, but I don't have kodi around to try on.
Maybe the mediastop needs to run in the background on the remote machine,
Code:
if ping -c 1 kodi."$machine".mylan.home ; then
ssh -i /path/to/key_ed25519 -f root@kodi."$machine".mylan.home "mediastop ${1} &"
fi
but the -f or & for the SSH client should have handled that regardless.
Ok. I think I've got it. I read all posts and changed some things. Also realized I think I made a mistake with the first try with the -f on the ssh. I didn't remove the &. As such the below configuration is working exactly how it should I think. Added the /dev/nulls as I don't need feedback unless I forget the parameter.
Run on the pxe booted machines via root user over ssh.
Code:
mediastop)
if [[ "$1" ]] ; then
for seconds in 30 20 10 ; do
kodi-send --action \
"Notification(kodi ${1} in ${seconds} seconds,standby!)" > /dev/null
sleep 10
done
kodi-send --action "${1}" > /dev/null
fi
exit 0
;;
And the script on the main server that fires to trigger the above.
Code:
mediastop)
if [[ "$HOSTNAME" == megalith ]] && [[ "$USER" == jason ]] ; then
if [[ "$1" ]] ; then
for machine in gst main mbr ; do
if ping -c 1 kodi."$machine".mylan.home > /dev/null ; then
ssh -f root@kodi."$machine".mylan.home "mediastop ${1}"
fi
done
else
echo "usage: ${SCRIPTCALL} | (powerdown|reboot)"
fi
fi
;;
Thank you for the direction. Will mark as solved. I am grateful to all.
Will also note that in my time waiting and working on this I also managed my wakeonlan script. Currently all 3 machines are starting and rebooting/powerdown within a few seconds of each other in the for loops as I hoped. Tested on crontab. Success.
Last edited by jmgibson1981; 12-09-2019 at 03:54 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.