6

In Ubuntu Linux 16.04.3, how do I run a script, dependant on another hard drive being mounted first, after fstab?

I have tried:

  • Creating a service script in /etc/init.d, but the script executes before fstab.
  • Adding to /etc/rc.local, but same problem.
LogicalException
  • 293
  • 1
  • 5
  • 11

1 Answers1

12

I believe you will need to use the newer systemd service on Ubuntu 16.04 rather than the /etc/init.d. Here you will create a service file to run your script after the local-fs.target.

let me present you with a sample of how this would be done.

  1. Create the needed service file in the /etc/systemd/system directory

  2. The file would look like this:

    [Unit]
    Description=Script to run after fstab
    After=local-fs.target
    
    [Service]
    Type=simple        
    ExecStart=/bin/bash -c "/script/to/run.sh"
    
    
    [Install]
    WantedBy=multi-user.target 
    
  3. And of course you will set up the service with:

    sudo systemctl start <name_of_service>
    sudo systemctl enable <name_of_service>
    

That would be how it could be done, please I haven't tested it myself, and here is link to documentation to help you further. Please look at other options available for the various fields in the service file.

https://access.redhat.com/articles/754933

https://www.freedesktop.org/software/systemd/man/bootup.html

https://www.freedesktop.org/software/systemd/man/systemd.html#

Note: that the noauto fstab mount option matters here see

 local-fs-pre.target
                |
                v
       (various mounts and   (various swap   (various cryptsetup
        fsck services...)     devices...)        devices...)       (various low-level   (various low-level
                |                  |                  |             services: udevd,     API VFS mounts:
                v                  v                  v             tmpfiles, random     mqueue, configfs,
         local-fs.target      swap.target     cryptsetup.target    seed, sysctl, ...)      debugfs, ...)
                |                  |                  |                    |                    |
                \__________________|_________________ | ___________________|____________________/
                                                     \|/
                                                      v
                                               sysinit.target
                                                      |
                 ____________________________________/|\________________________________________
                /                  |                  |                    |                    \
                |                  |                  |                    |                    |
                v                  v                  |                    v                    v
            (various           (various               |                (various          rescue.service

                |                  |                  |                                         v
                v                  v                  v                                 emergency.target
            display-        (various system    (various system
        manager.service         services           services)
                |             required for            |
                |            graphical UIs)           v
                |                  |           multi-user.target
                |                  |                  |
                \_________________ | _________________/
                                  \|/
                                   v
                         graphical.target

Looking at the diagram above your targets would be local-fs.target or sysinit.target not entirely sure as the latter covers all file systems. So you decide on the phase when your script would run to give you the desired effect.

You can also set a specific mountpoint as a target foobar.mount. Find your complete list with systemctl list-units --type=mount.

tuxone
  • 103
George Udosen
  • 36,677
  • 1
    Thanks for the answer and does seem straight-forward to understand. I'll try it tomorrow (when I'm awake) then mark as accepted answer if all goes well. – LogicalException Sep 11 '17 at 00:58
  • 1
    Looks good @george Though it will be possible with the older method too. "dependant on another hard drive being mounted first," This can be done with a udev rule too. – Rinzwind Sep 13 '17 at 08:26
  • 2
    This didn't work for me out of the box. The top [Install] section should be called [Unit] which contains the After= specification. Also I found that the /bin/bash -c part wasn't needed in ExecStart. Also for knowledge, simple effectively starts the process in the background – ddrake12 Jan 26 '18 at 18:11