Automate permission change tasks


I'm trying to find how to make a script to automate permission changes and then program it with cron.

I have created a directory in / home with a group and certain permissions, and I have added the users to that group.

As you know, if users create directories and / or files within this directory, they will inherit these permissions and group. The problem comes when users "paste" files already created, the permissions do not change.

I want to run a task that changes permissions and group every x time, but I do not know how to program it.

I imagine that I have to make a script, of this I do not have much idea, with the following:

sudo chgrp -R grupo /home/directorio
chmod -R 774 /home/directorio

And then schedule that script with cron. I have the problem with the script. I do not know how to write it and apply root permissions, keeping in mind that it has to be run in unattended mode.

asked by BeaSTR 22.01.2016 в 00:36

1 answer


It would be necessary to know what version of cron you are using. But I will try to give you an explanation that works for any version.
All these commands are like superuser:

We created the script:

echo "#!/bin/bash" > /usr/local/bin/
echo "chgrp -R grupo /home/directorio" >> /usr/local/bin/
echo "chmod -R 774 /home/directorio" >> /usr/local/bin/

We make the script executable only by the superuser:

chmod 0700 /usr/local/bin/

The following line is applicable only if you are using vixie-cron. We add a line to / etc / crontab for that script to run every 30 minutes:

echo "0,30 * * * *   root /usr/local/bin/" >> /etc/crontab

And that's it, vixie-cron automatically recognizes the changes in / etc / crontab without having to run crontab.

If you use a cron other than vixie-cron then do not do the last line and instead do the following.

Run as superuser:

crontab -e

An editor appears with the current crontab. Add the following line and close the editor by saving the file.

0,30 * * * *   /usr/local/bin/

You can also use crontab -e with vixie-cron; but the line to add must be the previous one, which includes root after the four asterisks.

answered by 22.01.2016 / 08:44