Search code examples
linuxservicebackgroundupstart

How to create folder if not exists in upstart script


This is my config file in /etc/init/

start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
# Automatically restart service
respawn
respawn limit 99 5
script
    mkdir /var/log/my_task
    <my task which log to /var/log/my_task>
end script

The problem is the service will try create folder every time. How to run the command only if the folder is not exist?


Solution

  • This code will work for you:

       start on local-filesystems
       stop on runlevel [!2345]
       respawn limit 99 5
       script
       if [ ! -e /var/log/my_task ]; then
         /bin/mkdir  /var/log/my_task
       fi
       end script