Cron and umask

Hi All,

Please help to understand what I am missing. The default umask as in /etc/profile is 022. The Script running in Cron creates file with umask 026. The default User shell is ksh and the script runs on bash.

How the Cron is creating the files with 026 umask, how to make it create files with 022.

Thanks for your time in answering.

Scripts running in cron get extremely minimal defaults. They don't load /etc/profile at all, they don't care what the user's shell is, etc, etc.

Nothing stops you from loading /etc/profile yourself however, either inside the crontab or inside the script itself.

0 * * * * . /etc/profile ; /path/to/script.sh

You could also put a umask there instead if that's all you really need.

@Crorona688 : Thanks for you quick response. Yes I can very well add umask/chmod, I am wondering how the umask is getting set when cron runs. Which is feeding the permission inputs to the Cron.

I don't want to add the Chmod/umask in the script. Rather I want make the change globally

Could this be the umask of the root user when it starts the crond daemon?

You don't say what OS you are running, but as a stab, have a look in /etc/init.d to see if you can find a script that starts crond. It might also be in /etc/inittab which might make things a little trickier. It might be possible to achieve what you want there and restart cron or maybe not.

There is a risk that in trying to change it, we might break something else, so be careful.

What OS and version are you running?

Robin

The only dependable things cron gives you are the SHELL, LOGNAME, and HOME environment variables as determined from that user's /etc/passwd entry. The rest just gets inherited from what cron had, and you do not want to mess with cron's own umask values -- that would have far-reaching unintended consequences.

If you require any particular setup, that is your application's responsibility. Loading /etc/profile in the crontab line itself is completely normal and expected. If you were expecting profile settings without loading the profile, that is your bug!

0 * * * * . /etc/profile ; /path/to/myscript.sh

Consider yourself lucky. On a rather obnoxious embedded system where cron didn't even set the UID right, I had to resort to sudo -i -u username /path/to/script to get the UID and setup I needed.

2 Likes