Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

This works for me:

check directory my_path path /mnt/some_mount_point
  if does not exist then exec "/usr/local/bin/"
share|improve this answer

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

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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