0

I'm hoping I can get some community help with a script I'm trying to pull together. I'm still learning here so I am prone to stupid mistakes.

I'm trying to achieve some automation when I mount a USB disk. Essentially I'd like some essential folders copied across and an inotify watch placed on a few folders that will move files across and delete empty directories as they are moved.

Everything works - except that when I try to run a command within all of this that would open a terminal automatically so that I can enter a password, the terminal does not open with systemd.

There doesn't appear to be a lot of info online on this, other than random env variables and alterations on the script of which I think I've tried enough to ask for help now.

Details and errors below:

Several scripts are used:

Systemd which fires at USB drive mount on the system:

elements-usb.service

[Unit]
Description=elements-usb-trigger
Requires=home-usb\x2dstorage.mount
After=home-usb\x2dstorage.mount

[Service]
Environment="DISPLAY=:0"
Environment="XAUTHORITY=/home/pi/.Xauthority"
ExecStart=/home/monster/.storage-scripts/elements-run-on-mount-SHELL
ExecStop=systemctl stop elements-usb.service

[Install]
WantedBy=graphical.target
WantedBy=home-usb\x2dstorage.mount

Execstart points to this code, which is where the terminal should be launched:

/home/monster/.storage-scripts/elements-run-on-mount-SHELL

#!/bin/bash
gnome-terminal -e "/bin/bash -c '/home/monster/.storage-scripts/elements-run-on-mount;exec $SHELL'"

This executes the script below...

#!/bin/bash
###--Move existing files to USB--###

#tv folder mover initial sync
rsync -avhP --remove-source-files /home/monster/Videos/htpc-server-tv/  /home/usb-storage/Video/tv
notify-send "R-Sync says" "Moving tv shows from tv shows"

find /home/monster/Videos/htpc-server-tv/ -type f -empty -exec rm {} \;
notify-send "TV folder says" "Cleaned :)"

#movies folder mover initial sync
rsync -avhP --remove-source-files /home/monster/Videos/htpc-server-movies/  /home/usb-storage/Video/movies
notify-send "R-Sync says" "Moving movies to movies folder"

find /home/monster/Videos/htpc-server-movies/ -type f -empty -exec rm {} \;
notify-send "Movie folder says" "Me too :)"

#R-Sync timeshift to usb copy
sudo rsync -avhP /home/timeshift/  /home/usb-storage/Backup/monster-laptop/timeshift --info=stats2&
notify-send "R-Sync says" "Timeshift backup of backup taking off"

#virtual server sync - htpc server to usb copy
sudo rsync -avhP /home/virtualbox/htpc-server/  /home/usb-storage/Backup/virtual servers/htpc-server --info=stats2&
notify-send 'R-Sync says' 'htpc-server backup of backup underway :)'

#virtual server sync - crypto server to usb copy
sudo rsync -avhP /home/virtualbox/crypto-server/  /home/usb-storage/Backup/virtual servers/crypto-server --info=stats2&
notify-send 'R-Sync says' 'crypto-server backup of backup underway :)'

####--WATCH FOLDERS--####
#NOTE: Requires inotify tools "sudo apt-get install inotify-tools"

#tv - move new videos to USB drive videos folder
sh /home/monster/.storage-scripts/inotify-script-watchenmove-videos-tv&
notify-send "R-Sync says" "Boom! tv"

#movie - move new videos to USB drive videos folder
sh /home/monster/.storage-scripts/inotify-script-watchenmove-videos-movies&
notify-send "R-Sync says" "Bam! - movie"

#Bind USB drive folders to home/videos/.usb-Videos to present to virtual machine
#sudo mount --bind '/home/usb-storage/Video' '/home/monster/Videos/.usb-Videos'&

This all seems to run fine when run as a script, but when launched by systemd it seems to fail quite badly.

Results from systemctl status elements-usb.service

● elements-usb.service - elements-usb-trigger
   Loaded: loaded (/etc/systemd/system/elements-usb.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sat 2019-08-24 11:20:51 BST; 31s ago
 Main PID: 13044 (code=exited, status=1/FAILURE)

Aug 24 11:20:51 monster-laptop systemd[1]: Started elements-usb-trigger.
Aug 24 11:20:51 monster-laptop elements-run-on-mount-SHELL[13044]: # Option “-e” is deprecated and might be removed in a later version
Aug 24 11:20:51 monster-laptop elements-run-on-mount-SHELL[13044]: # Use “-- ” to terminate the options and put the command line to ex
Aug 24 11:20:51 monster-laptop elements-run-on-mount-SHELL[13044]: No protocol specified
Aug 24 11:20:51 monster-laptop elements-run-on-mount-SHELL[13044]: Unable to init server: Could not connect: Connection refused
Aug 24 11:20:51 monster-laptop elements-run-on-mount-SHELL[13044]: # Failed to parse arguments: Cannot open display:
Aug 24 11:20:51 monster-laptop systemd[1]: elements-usb.service: Main process exited, code=exited, status=1/FAILURE
Aug 24 11:20:51 monster-laptop systemd[1]: elements-usb.service: Failed with result 'exit-code'.

**Seems to fail due to some graphic / GUI issue with launching the terminal.

If the systemd process kicked off the script alone, i.e. /home/monster/.storage-scripts/elements-run-on-mount

rather than:

gnome-terminal -e "/bin/bash -c '/home/monster/.storage-scripts/elements-run-on-mount;exec $SHELL'"

It runs just fine at USB drive mount, but there are no visual cues as to what it's up to and some of the R-Sync functions can take a little time.

There is likely a further question about the use of sudo in the script since systemd "I think" runs scripts as root anyway and perhaps a little adjustment there might also be required.

Output of journalctl -xe

    Aug 24 11:27:50 monster-laptop gjs[5062]: g_object_unref: assertion 'G_IS_OBJECT (object)' failed
Aug 24 11:27:50 monster-laptop gjs[5062]: g_object_unref: assertion 'G_IS_OBJECT (object)' failed
Aug 24 11:28:48 monster-laptop gnome-shell[4130]: [night-light-slider] Setting night light schedule from 5 to 17
Aug 24 11:30:01 monster-laptop CRON[17646]: pam_unix(cron:session): session opened for user root by (uid=0)
Aug 24 11:30:01 monster-laptop CRON[17647]: (root) CMD ([ -x /etc/init.d/anacron ] && if [ ! -d /run/systemd/system ]; then /usr/sbin/
Aug 24 11:30:01 monster-laptop CRON[17646]: pam_unix(cron:session): session closed for user root
Aug 24 11:31:15 monster-laptop org.gnome.Shell.desktop[4130]: [9934:9934:0824/113115.982679:ERROR:buffer_manager.cc(488)] [.DisplayCom
Aug 24 11:32:04 monster-laptop systemd[1]: Started Run anacron jobs.
-- Subject: A start job for unit anacron.service has finished successfully
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- A start job for unit anacron.service has finished successfully.
-- 
-- The job identifier is 6098.
Aug 24 11:32:04 monster-laptop anacron[18538]: Anacron 2.3 started on 2019-08-24
Aug 24 11:32:04 monster-laptop anacron[18538]: Normal exit (0 jobs run)
Aug 24 11:32:04 monster-laptop systemd[1]: anacron.service: Succeeded.
-- Subject: Unit succeeded
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- The unit anacron.service has successfully entered the 'dead' state.
lines 1747-1769/1769 (END)

So, please guys and gals - can you help me fix my script firstly?

Also, while I'm asking for direct help on the terminal launch - if you spot an improvement, I'd love your advice.

Thanks


● elements-usb.service - elements-usb-trigger
   Loaded: loaded (/etc/systemd/user/elements-usb.service; static; vendor preset: enabled)
   Active: inactive (dead)

Aug 26 08:58:58 monster-laptop systemd[5099]: Starting elements-usb-trigger...
Aug 26 08:58:58 monster-laptop gnome-terminal[8125]: # Option “-e” is deprecated and might be removed in a later versio
Aug 26 08:58:58 monster-laptop gnome-terminal[8125]: # Use “-- ” to terminate the options and put the command line to e
Aug 26 08:58:58 monster-laptop gnome-terminal[8125]: # _g_io_module_get_default: Found default implementation gvfs (GDa
Aug 26 08:58:58 monster-laptop gnome-terminal[8125]: # _g_io_module_get_default: Found default implementation dconf (DC
Aug 26 08:58:58 monster-laptop gnome-terminal[8125]: # watch_fast: "/org/gnome/terminal/legacy/" (establishing: 0, acti
Aug 26 08:58:58 monster-laptop gnome-terminal[8125]: # unwatch_fast: "/org/gnome/terminal/legacy/" (active: 0, establis
Aug 26 08:58:58 monster-laptop gnome-terminal[8125]: # watch_established: "/org/gnome/terminal/legacy/" (establishing: 
Aug 26 08:58:59 monster-laptop systemd[5099]: elements-usb.service: Succeeded.
Aug 26 08:58:59 monster-laptop systemd[5099]: Started elements-usb-trigger.

TBH not sure where to from here. Any ideas?

1
  • What happens when you source the main script? Like this #!/bin/bash gnome-terminal -e "/bin/bash -c '. /home/monster/.storage-scripts/elements-run-on-mount;exec $SHELL'" – Mitch Aug 26 '19 at 5:12
0

The service unit file outlined below works on my Ubuntu 18.04 desktop (systemd 237), i.e. it opens a terminal window when I start it manually with a systemctl command. Of course, to adjust it to your needs (usb-mount trigger, calling the script, ..) you need to modify it in various places with bits and pieces from the service file you already have.

As you already suspected, the original service unit file did not work, because there was a problem with importing environmental variables and with trying to open a new GUI session, that is a new Gnome terminal window.

The most important changes to make it work:

  1. You need to run or invoke the service unit as a systemd user service.
    This is because starting a terminal session with a systemd system service is regarded unsafe and in some distros even blocked by default. The way systemd distinguishes between user and system unit files is by file location. By placing a service unit file (like the one below) in, for example, the /etc/systemd/user directory it will be recognized by systemd as a user service file (for more info on which directories can be used see the systemd man pages). For testing purposes you can manually start the service with

    systemctl --user start servicefilename.service
    
  2. Add the ExecStartPre directive to enable import of the environment variables. This is only one way to do it. There are other options that may be more suited to your case (see here).

  3. (optional) run the service as Type=oneshot. This way it will only run once when triggered and you do not need an ExecStop directive. Again, this is only one way to do it.

    [Unit]  
    Description=elements-usb-trigger  
    Requires=default.target  
    After=default.target  
    
    [Service]  
    Type=oneshot  
    ExecStartPre=/bin/systemctl --user import-environment  
    ExecStart=/usr/bin/gnome-terminal -e "/bin/bash -c 'echo hello;exec $SHELL'"  
    
    [Install]  
    \# optional dependencies
    

Please note that there seems to be some works in the pipeline that may change/simplify things in the near (?) future.

12
  • Thank you for the very thoughtful response - I'll try this in the evening and report back – Fredfree Aug 25 '19 at 16:40
  • I'm afraid I'm still getting a failure when I mount/insert the drive. The systemctl log shows the same message as before. – Fredfree Aug 26 '19 at 0:47
  • Did you try to run a simple service file like the one I provided and see what that gives? (don't forget systemctl --user daemon-reload) If it fails too, then the reason why we see different results might simply be related to different systemd versions. Although I am amazed by how easy it is in many cases to more or less copy a unit file from somewhere and run it without a glitch on a different system, there is of course no reason to take that for granted in all cases. – yesno Aug 26 '19 at 5:53
  • Just ran systemctl --user daemon-reload and plugged the drive in again - it still fails, but with a different error message. Now I'm getting, "monster-laptop systemctl[30586]: Failed to connect to bus: No such file or directory" so it appears to have changed and there is a new related issue to solve somehow? – Fredfree Aug 26 '19 at 7:48
  • So it seems that running "systemctl --user status elements-usb.service" brings different results. I guess this is where to look for the --user service log now? It would seem that the service isn't getting triggered or get's triggered once. I'm posting the results as an answer to the question – Fredfree Aug 26 '19 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.