1

In the monit documentation I can see there is a check directory command but I can't see any examples of how to use it to see if the directory exists and if it doesn't to create the directory using the mkdir command.

Paul Sturgess
  • 111
  • 1
  • 4

4 Answers4

6

This works for me:

check directory my_path path /mnt/some_mount_point
  if does not exist then exec "/usr/local/bin/create_mnt.sh"
Alec Thomas
  • 161
  • 1
  • 3
2

This might help you mkdir -p $directory should do what you want. The -p option will create any necessary parent directories. If $directory already exists as a directory, the command does nothing, and succeeds. If $directory is a regular file, it will remain untouched, nd does not fail if the directory already exists, so you can do it all at once with:

mkdir -p /some/directory/you/want/to/exist || exit 1

TBI Infotech
  • 1,536
  • 9
  • 15
0

Alec pretty much has it, but you don't need to create a script to do this. You can run mkdir directly (though note you need the full path to it):

check directory my_path path /mnt/some_mount_point
  if does not exist then exec "/bin/mkdir /mnt/some_mount_point"

Depending on your path, it may help to add the -p switch to mkdir if you need subfolders and/or duplicate error suppression.

Synchro
  • 2,983
  • 5
  • 25
  • 35
0

No need to create separate script, you need to use bash with -c option:

check directory my_path path /some/path/to/dir
  if does not exist then exec "/bin/bash -c 'mkdir -p /some/path/to/dir'"
  as uid <some_user> and gid <some_group>

The monit process is run from the root user, so make sure you are not creating directories as root (supposedly you don't need that) And then you can kill monit process and start it in foreground with

monit -Ivv

and see output debug notes. Once there are not errors you can stop monit in foreground with C-c and start it as usual deamon process.

Hope this helps.

Developer
  • 101
  • 1