icy
icy

Reputation: 1446

Create cronjob ubuntu for user www-data using bash

How can I create new cronjob for user www-data using bash script?

This code works, but create the cronjob for root user (I must run this script as root)

crontab -l | { cat; echo "0 1 * * * curl -L http://localhost/cronjob #my cronjob description"; } | crontab -

Upvotes: 1

Views: 1046

Answers (3)

Barbaros Özhan
Barbaros Özhan

Reputation: 65363

assuming you use linux : Go to /etc directory(when you're at root user)

# cd /etc

Delete both these files :

# rm cron.deny

# rm at.deny

Open cron.allow :

# vi cron.allow

Add one line for each user allowed to use the crontab command to create cron jobs, such as:

john
linda
www-data

Finally, edit at.allow using a text editor such as vi, enter:

# vi at.allow

Add one line for each user allowed to use the at command to create at jobs, again such as:

john
linda
www-data

Upvotes: 0

nbari
nbari

Reputation: 26965

Give a try to :

crontab -e -u www-data

For doing this you will need to be root, from the man page:

If the -u option is given, it specifies the name of the user whose crontab is to be used (when listing) or modified (when editing).

The  -e  option  is  used  to edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables.

You can also add the cron to the crontabs /var/spool/cron/crontabs

There is one file for each user's crontab under the /var/spool/cron/crontabs directory. Users are not allowed to edit the files under that directory directly to ensure that only users allowed by the system to run periodic tasks can add them, and only syntactically correct crontabs will be written there.

In your case would be /var/spool/cron/crontabs/www-data

Upvotes: 2

user2940867
user2940867

Reputation:

Try this (required privileged user)

echo "0 1 * * * curl -L http://localhost/cronjob #my cronjob description" >> /var/spool/cron/crontabs/www-data

Upvotes: 1

Related Questions