LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-09-2019, 11:34 AM   #1
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,140

Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392
Using & in bash. How to exit properly.


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.
 
Old 12-09-2019, 11:51 AM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546
Quote:
Originally Posted by jmgibson1981 View Post
to run as a non root user i run the ssh with an ampersand.
Isn't this what sudo is for?


Are you certain it's the ssh line causing the hanging? (i.e. if you comment out that one line does the script return ok?)
 
Old 12-09-2019, 11:57 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,306
Blog Entries: 3

Rep: Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720
I'd look at the -f option for ssh or else let ssh run its course without the &.
 
Old 12-09-2019, 02:00 PM   #4
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,140

Original Poster
Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392
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.
 
Old 12-09-2019, 02:09 PM   #5
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
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.
 
Old 12-09-2019, 02:21 PM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,306
Blog Entries: 3

Rep: Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720
Again, see the -f option:

Code:
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.
 
1 members found this post helpful.
Old 12-09-2019, 02:33 PM   #7
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
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?

Last edited by scasey; 12-09-2019 at 02:35 PM.
 
Old 12-09-2019, 02:40 PM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,306
Blog Entries: 3

Rep: Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720
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.

Code:
while read i; do 
        echo $i; 
        ssh -i ~/.ssh/key -f xx.yy.zz.aa 'sleep 2; date;' ; 
done < <(seq 1 3); 
echo foo;
However, if you want to be able to pause the script until everything completes, then & is the way to go.

Code:
while read i; do 
        echo $i; 
        ssh -i ~/.ssh/key xx.yy.zz.aa 'sleep 2; date;' & 
done < <(seq 1 3); 
wait; 
echo foo;
 
1 members found this post helpful.
Old 12-09-2019, 02:48 PM   #9
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Hmm. What about -n then?
From man ssh
Code:
 n' Redirects stdin from /dev/null (actually, prevents reading from stdin).
 This must be used when ssh is run in the background
Emphasis added.

Note that the OP reported that -f didn’t solve their problem.
Still wondering about adding an exit to the second case...

Last edited by scasey; 12-09-2019 at 02:51 PM.
 
Old 12-09-2019, 02:59 PM   #10
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,306
Blog Entries: 3

Rep: Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720
The -n is implied by the -f.

Quote:
Originally Posted by scasey View Post
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.
 
1 members found this post helpful.
Old 12-09-2019, 03:40 PM   #11
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,140

Original Poster
Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 05:20 PM
AOL UK && BT Voyager 100 && Slackware 10.2 && RP-PPPoE pitt0071 Linux - Networking 3 01-17-2006 06:10 AM
Ph&#7909;c h&#7891;i d&#7919; li&#7879;u b&#7883; m&#7845;t???, c&#7913; pollsite General 1 06-27-2005 12:39 PM
Gotta love those &#1649;&#1649;&#1649;&#1649;&#1649;&#1649;&#1649;&# iLLuSionZ Linux - General 5 11-18-2003 07:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration