1

I'm running a software which needs specific rights for different subfolders. Therefore I'm using crontab to update those rights permanently.

On one server I'm using a perl-script which groups all the commands and which is executed one by crontab.

crontab entry:

* * * * * root     /root/Permissions.sh /dev/null 2&1

Permissions.sh:

#!/usr/bin/perl
system ("chmod -R 755 /pathToMainFolder/softwareFolder");
system ("chown -R root:user /pathToMainFolder/softwareFolder");
system ("chmod -R 775 /pathToMainFolder/softwareFolder/subfolder");
system ("chown -R root:user /pathToMainFolder/softwareFolder/subfolder");
system ("chmod -R 775 /pathToMainFolder/softwareFolder/someFile");
system ("chown -R root:user /pathToMainFolder/softwareFolder/someFile");
system ("chmod 775 /pathToMainFolder/softwareFolder");

This works fine, but on another server, the software is located in a subfolder whose name includes a space

/pathToMainFolder/sub Folder/softwareFolder

I don't know how to write the Permissions.sh to include this space, as with and without escaping the space, the script does not seem to work.

Neither:

system ("chmod -R 755 /pathToMainFolder/sub Folder/softwareFolder");

nor:

system ("chmod -R 755 /pathToMainFolder/sub\ Folder/softwareFolder");

seems to work.

I then tried to simply copy the 7 commands directly into crontab:

* * * * * root chmod -R 755 /pathToMainFolder/sub\ Folder/softwareFolder
* * * * * root chown -R root:user /pathToMainFolder/sub\ Folder/softwareFolder
* * * * * root chmod -R 775 /pathToMainFolder/sub\ Folder/softwareFolder/subfolder
* * * * * root chown -R root:user /pathToMainFolder/sub\ Folder/softwareFolder/subfolder
* * * * * root chmod -R 775 /pathToMainFolder/sub\ Folder/softwareFolder/someFile
* * * * * root chown -R root:user /pathToMainFolder/sub\ Folder/softwareFolder/someFile
* * * * * root chmod 775 /pathToMainFolder/sub\ Folder/softwareFolder

This helped somehow, as they where now executed properly, but crontab does not seem to respect the sequence of the commands.

Mar  1 10:19:01 local CRON[53069]: (root) CMD (chown -R root:user /pathToMainFolder/sub\ Folder/softwareFolder/subfolder)
Mar  1 10:19:01 local CRON[53070]: (root) CMD (chown -R root:user /pathToMainFolder/sub\ Folder/softwareFolder/someFile)
Mar  1 10:19:01 local CRON[53071]: (root) CMD (chmod -R 775 /pathToMainFolder/sub\ Folder/softwareFolder/subfolder)
Mar  1 10:19:01 local CRON[53073]: (root) CMD (chown -R root:user /pathToMainFolder/sub\ Folder/softwareFolder)
Mar  1 10:19:01 local CRON[53072]: (root) CMD (chmod -R 775 /pathToMainFolder/sub\ Folder/softwareFolder/someFile)
Mar  1 10:19:01 local CRON[53074]: (root) CMD (chmod -R 755 /pathToMainFolder/sub\ Folder/softwareFolder)
Mar  1 10:19:01 local CRON[53075]: (root) CMD (chmod 775 /pathToMainFolder/sub\ Folder/softwareFolder)

As the different commands work recursively, it is crucial that they are executed in the right order.

I found https://stackoverflow.com/questions/9639434/running-two-commands-sequentially-in-a-cron-job and https://help.ubuntu.com/community/CronHowto, so I tried to use && to execute the chown and chmod commands sequentially, but somehow this does not work at all.

* * * * * root chmod -R 755 /pathToMainFolder/sub\ Folder/softwareFolder && root chmod -R 775 /pathToMainFolder/sub\ Folder/softwareFolder/subfolder && root chmod -R 775 /pathToMainFolder/sub\ Folder/softwareFolder/someFile && root chmod 775 /pathToMainFolder/sub\ Folder/softwareFolder
* * * * * root chown -R root:user /pathToMainFolder/sub\ Folder/softwareFolder && root chown -R root:user /pathToMainFolder/sub\ Folder/softwareFolder/subfolder && root chown -R root:user /pathToMainFolder/sub\ Folder/softwareFolder/someFile

I don't get an error message, but doing an ls -al of the folders showed that the commands are not executed correctly.

Is there any way to fix the Permissions.sh, which I think would be the best way to get this working again, or where else did I make a mistake.

Note: I could move the whole software out of the sub Folder, but then I would have to manually change links on many workstations, which would be a pain in the ***.

1 Answer 1

2
  • why perl and not bash?
  • Using bash and doublequotes.

    #/bin/bash
    chmod -R 755 "/pathToMainFolder/sub Folder/softwareFolder"
    chown -R root:user "/pathToMainFolder/sub Folder/softwareFolder"
    chmod -R 775 "/pathToMainFolder/sub Folder/softwareFolder/subfolder"
    chown -R root:user "/pathToMainFolder/sub Folder/softwareFolder/subfolder"
    chmod -R 775 "/pathToMainFolder/sub Folder/softwareFolder/someFile"
    chown -R root:user "/pathToMainFolder/sub Folder/softwareFolder/someFile"
    chmod 775 "/pathToMainFolder/sub Folder/softwareFolder"
    

    Save and execute this script from cron.

1
  • Thanks, this works. This 1st line is only 755 while the 3rd is 775. All files and folders except 'subfolder' an 'someFile' need 755.
    – Pit
    Commented Mar 1, 2016 at 10:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .