Where do put script to run scp on logout, restart, logoff from GNOME

General support questions
Post Reply
justlearning
Posts: 3
Joined: 2017/02/06 17:07:05

Where do put script to run scp on logout, restart, logoff from GNOME

Post by justlearning » 2017/02/06 18:20:08

I am running CentOS 7 and have 2 bash scripts to do a scp from another CentOS 7 box. One to get some user preferences on startup of Gnome and one to save on logout, reboot, shutdown of Gnome. On startup I have the file in .config/autostart/getPrefs.desktop which works great. When starting gnome it runs the script and copies the files just fine.
On logout I am not having any success. I am able to invoke the script via terminal successfully. I can get the script to write a text file log that it completed successfully to the user desktop on shutdown and reboot and even make a local copy.
I have spent a lot of time on this site reviewing similar issues but have not been able to get any to work.

on startup in ~/.config/autostart/getPrefs.desktop
[Desktop Entry]
Type=Application
Exec=/home/kiosk/.getPrefs
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
...

the .getPrefs
#!/bin/sh -x
scp -r veger@10.0.1.28:/home/veger/data/Default /home/kiosk/.config/google-chrome/

on shutdown, reboot, logout and poweroff I want to run just the reverse
.savePrefs
#!/bin/sh -x
scp -r /home/kiosk/.congig/google-chrome/Default veger@10.0.1.28:/home/veger/data/

I have tried /etc/gdm/PostSession/Default
I have tried /etc/rc1-5.d with executable script at /etc/init.d
I have tried /etc/systemd/system/

This seems like there should be a simple solution as is for autostart and most of the samples here are for older versions of CentOS. I am not afraid to do the work but would love to be pointed in the right direction as I am new to CentOS.

Thank you in advance.

justlearning
Posts: 3
Joined: 2017/02/06 17:07:05

Re: Where do put script to run scp on logout, restart, logoff from GNOME

Post by justlearning » 2017/02/08 15:56:51

I found a solution that works for me on logout of GNOME.
The problem I was having was the script I was running needed to run as a specific user and the GNOME logout script located at /etc/gdm/PostSession/Default runs as root.
To fix my problem so that the script runs either from the GNOME or SHELL I put the call to the script for my SCP in the user .bash_logout
I call the .bash_logout from the GNOME PostSession. I can not take credit for this idea, I read it in a book preview from Linux Timesaving Tips and Tricks for Dummies.

/etc/gdm/PostSession/Default
#!/bin/sh
if [ $USER = "username" ];then
runuser -l username -c 'bash /home/user/.bash_logout'
sleep 1
fi
exit 0

The runuser will work by itself, but it will try and do that for every user profile that logs out of GNOME, but testing for the user only the .bash_logout of that user is called on logout.
I am new to Linux and rely heavily on these types of forums thank you to everyone who takes time to post.

justlearning
Posts: 3
Joined: 2017/02/06 17:07:05

Re: Where do put script to run scp on logout, restart, logoff from GNOME

Post by justlearning » 2017/02/10 14:46:08

To finish this up I also found a method that is working for me on shutdown and reboot that will run the scp script.
I have in my local space "/home/kiosk" created a skywalker.sh which has the same runuser command that is in the /etc/gdm/PostSession/Default
I added a service at /etc/systemd/system/save-prefs.service

[Unit]
Description=The_Force_Is_Strong_With_This_One
After=network.target

[Service]
RemainAfterExit=true
ExecStop=/home/kiosk/skywalker.sh
Type=oneshot

[Install]
WantedBy=default.target shutdown.target reboot.target halt.target


On shutdown and restart this runs as root. It calls the skywalker.sh which does a
runuser -l kiosk -c 'bash /home/kiosk/.bash_logout'
and that runs the script with the secure copy command

Post Reply