You mentioned your user is ssh
ing in, not logging in locally. So the use-ssh-agent
in /etc/X11/Xsession.options
is a red herring: it won't be executed on SSH sessions, only when logging into a X11 GUI desktop locally (or using some virtual X11 session like over VNC or RDP).
Instead, you should check if libpam-ssh
is installed on either system. It can be configured to authenticate a user using SSH private key passphrases, but that is optional and you'll need to specifically place the key to ~/.ssh/login-keys.d/
for that functionality.
Its other feature, though, is to auto-start a SSH agent on any login session and automatically add SSH private keys to the agent if their passphrase is the same as the user's login password. I'm thinking this might be the cause of the different behavior between your systems.
You can run a process that will send a signal (e.g. SIGALRM
) to the shell script every x time, and use a trap for this signal. This process could be a script doing something like:
set -e
while true
do
sleep 300
kill -ALRM $PPID
done
if started by the main shell script.
The main shell script should kill this process when it is no longer needed, and/or the process should terminate when the pid no longer exists (however there's a race condition on that).
(EDITED) Note: If your main shell script uses the sleep
command, it may badly interact with the ALRM
signal if sleep
is a builtin and sleep(3)
is implemented with alarm(2)
. The POSIX description of the sleep
shell utility also says in its rationale: "The exit status is allowed to be zero when sleep is interrupted by the SIGALRM signal because most implementations of this utility rely on the arrival of that signal to notify them that the requested finishing time has been successfully attained. Such implementations thus do not distinguish this situation from the successful completion case. Other implementations are allowed to catch the signal and go back to sleep until the requested time expires or to provide the normal signal termination procedures." To avoid potential issues with some implementations, you can use SIGUSR1
or SIGUSR2
instead of SIGALRM
.
Here's an example using a subshell. To make the behavior easier to see, I've replaced the 5-minute period (sleep 300
) by a 5-second period (sleep 5
).
#!/bin/sh
{
set -e
while true
do
sleep 5
# Since this is run as a subshell (instead of an external command),
# the parent pid is $$, not $PPID.
kill -USR1 $$
done
} &
trap 'echo "Signal USR1 received."' USR1
while true
do
date
sleep 1
done
The script can be interrupted with Ctrl-C. It doesn't kill the subshell when this happens, but if the pid isn't reused, the subshell terminates automatically after no more than the period (here, 5 seconds) because the kill
command fails (kill: No such process
).
Best Answer
From the bash manual: