You are not logged in.

#1 2017-09-01 08:45:07

edriv
Member
Registered: 2013-03-13
Posts: 13

[SOLVED] Script fails to mount a device when called by a timer unit

I've coded a script that checks if a /dev file is present, and possibly mounts it.
Done this way because I've tried other automount possibilities with no success, and the only solution that
worked fine to mount my Android smartphone was the command "go-mtpfs".
Since I wanted this to be automatized, created a timer unit for systemd that every 5 seconds runs the script.
This fails the mounting, but I've checked that:
- the timer unit actually runs the script
- when runned by hand, it performs the mount

What could prevent it from working? I wonder if it's a similar problem to the fact that mount commands can't be
run directly from udev rules.

Last edited by edriv (2017-09-02 18:09:14)

Offline

#2 2017-09-01 08:50:59

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: [SOLVED] Script fails to mount a device when called by a timer unit

Paste the script, the service file and timer, and the relevant journal output.
See How To Ask Questions The Smart Way.



Moving to NC...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2017-09-01 10:52:25

edriv
Member
Registered: 2013-03-13
Posts: 13

Re: [SOLVED] Script fails to mount a device when called by a timer unit

The script: ~/.android_automounter.sh

#!/bin/sh
#udev creates this file when my device is connected
ANDROID_DEVFILE=/dev/android_adb
#lock variable to avoid mounting multiple times
IPC_VARIABLE=/tmp/android_ismounted

if [ -e $ANDROID_DEVFILE ]
then
  echo "FILE FOUND"
  if [ -e $IPC_VARIABLE ]
  then
    ANDROID_MOUNTED=`cat $IPC_VARIABLE`
  else
    ANDROID_MOUNTED=0
    echo "0" > $IPC_VARIABLE
  fi

  if [ $ANDROID_MOUNTED -eq 0 ]
  then
    echo "MOUNTING"
    go-mtpfs /mnt/android &
    echo "1" > $IPC_VARIABLE
  fi
else
  echo "FILE NOT FOUND"

  if [ -e $IPC_VARIABLE ]
  then
    ANDROID_MOUNTED=`cat $IPC_VARIABLE`
  else
    ANDROID_MOUNTED=0
    echo "0" > $IPC_VARIABLE
  fi
  if [ $ANDROID_MOUNTED -eq 1 ]
  then
    fusermount -u /mnt/android
    echo "0" > $IPC_VARIABLE
  fi
fi

The units:
/etc/systemd/user/android.timer   

[Unit]
Description=Checks and optionally mounts a virtual fs

[Timer]
OnBootSec=1min
OnUnitActiveSec=5

[Install]
WantedBy=timers.target

/etc/systemd/user/android.service   

[Unit]
Description=Mounts a specific device

[Service]
ExecStart=~/.android_automounter.sh

Some tests:

[elda@PCCASA ~]$ echo 0 > /tmp/android_ismounted 
(after 10 secs)
[elda@PCCASA ~]$ cat /tmp/android_ismounted 
1
[elda@PCCASA ~]$ ls /mnt/android
[elda@PCCASA ~]$
[elda@PCCASA ~]$ echo 0 > /tmp/android_ismounted 
(before 5 secs)
[elda@PCCASA ~]$ sh .android_automounter.sh
FILE FOUND
MOUNTING
[elda@PCCASA ~]$ 2017/09/01 12:45:48 FUSE mounted

I explain the first two commands as testing if the script has been run; "ls" gives that the device
has not actually been mounted, in the following commands I manually run the script with success.

Offline

#4 2017-09-01 15:04:20

berbae
Member
From: France
Registered: 2007-02-12
Posts: 1,304

Re: [SOLVED] Script fails to mount a device when called by a timer unit

What happens if you use the 'Type=forking' for android.service ?

I have doubts about the 'go-mtpfs /mnt/android &' line in the script: do you know why go-mtpfs is run in background?

Offline

#5 2017-09-01 15:12:58

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,545
Website

Re: [SOLVED] Script fails to mount a device when called by a timer unit

ExecStart=~/.android_automounter.sh

There is no such file as ~/.android_automounter.sh

There are (at least) 2 reasons this fails: 1) ExecStart needs a file to Exec when it Starts.  The tilde is not an actual path, it is a feature of the shell, but you are not telling systemd to run a shell, so there is nothing to expand that tilde into your home directory.  2) even if you did tell systemd to run a shell, the tilde would not be expanded to your home directory, but rather to /root/ as systemd services run as root.

Use the full path to the script.

I'd not be surprised if there are yet other problems.  Mounting from within a script launched by systemd may or may not work at all as all subprocesses end up killed and cleaned up after.  But at very least, fixing the 2 above will allow you to find out.

Last edited by Trilby (2017-09-01 15:14:13)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#6 2017-09-02 18:08:31

edriv
Member
Registered: 2013-03-13
Posts: 13

Re: [SOLVED] Script fails to mount a device when called by a timer unit

berbae wrote:

What happens if you use the 'Type=forking' for android.service ?

I have doubts about the 'go-mtpfs /mnt/android &' line in the script: do you know why go-mtpfs is run in background?

With this adjustment it works fine.

@Tribly: you're right, but actually I had edited the code to hide my computer's username without having this in mind.

Offline

Board footer

Powered by FluxBB