[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: SOLVED Re: Cannot rum multiple command on remote machine via SSH



On Wed, Feb 08, 2023 at 12:00:13PM +0000, Ottavio Caruso wrote:
> $ cat opt/bin/hibernate.sh
> mate-screensaver-command -l 	# Activates screensaver and locks
>                                   the screen
> sudo systemctl hybrid-sleep	# Hibernate and suspend the system. This
>                                   will trigger activation of
>                                   the special target unit
>                                   hybrid-sleep.target.

1) Missing the shebang.

2) The online man page for mate-screensaver-command says it:
   "Tells the running screensaver process to lock the screen immediately"
   Unfortunately, it doesn't say what happens if there's no running
   mate-screensaver process.  Does it start one as a foreground child
   or something?

3) systemctl(1) says that hybrid-sleep "is asynchronous, and will return
   after the hybrid sleep operation is successfully enqueued. It will not
   wait for the sleep/wake-up cycle to complete."

So it's not clear which command is causing the ssh client to hang.
My instinct says it's more likely to be mate-screensaver-command, but
I have zero experience with it.

Maybe you could try something like this:

==============================================================================
#!/bin/sh
log=~/.cache/hibernate.log
mkdir -p ~/.cache
test -f "$log" || touch "$log"

tail -n0 -f "$log" & pid=$!

# Activate screensaver and lock the screen.
mate-screensaver-command -l >>"$log" 2>&1

# Hibernate and suspend the system.
# This triggers the special unit hybrid-sleep.target.
sudo systemctl hybrid-sleep >>"$log" 2>&1

sleep 3
kill "$pid"
wait
==============================================================================

Feel free to adjust the sleep time as you see fit.  I've tried hard to
minimize race conditions, but they're not completely avoidable here.
If it takes more than 3 seconds for mate-screensaver-command and
systemctl to do their things, then you may have to sleep a bit longer.

If this hangs the ssh client, then we'll need to know which command
is still running.  That would typically mean logging in with a
second ssh command, finding out which tty the script is running on
(e.g. ps -ef | grep hibernate), then seeing what commands are running
on that tty (e.g. ps -ft pts/2).  At least, that's where I would start.


Reply to: