0

I need to manually create a directory using the following command in a cronjob and not have to enter in a password:

sudo mkdir /fold1/

I have read that I should not edit /etc/sudoers directly.

What are my options?

4
  • What does your system administrator say about editing /etc/sudoers? Do they prefer to use a file in /etc/sudoers.d/ ? Do you need to use sudo without changing the existing sudoers privileges? Commented Mar 9, 2020 at 14:21
  • 2
    Why do you need to use sudo inside the cron job? Does your system not support running cron jobs as other users (via /etc/crontab, or sudo crontab -e for example)? Commented Mar 9, 2020 at 14:24
  • 3
    Often "I should not edit /etc/sudoers directly" means simply "I have to use the visudo command to edit /etc/sudoers". Please check man visudo. Commented Mar 9, 2020 at 14:44
  • You asked explicitly about cronjob why this is not a direct answer. But you could serialze it via systemd-unit and set the run-user to root. Commented Mar 9, 2020 at 19:57

1 Answer 1

1

Add the script to root's cronjob:

sudo crontab -e

You need to be a sudoer to run the above command. You also will still have to cd to the correct directory inside your script to create your new directory in the right location.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.