0

I wish to execute a script every time on boot-up using /etc/rc.local.

My script for example is called startscript.sh which is stored in /home/debian as below. It first tries to create a file called test.log and then does other things.

However, I read the error on boot-up that touch: cannot touch ‘test.log’: Permission denied

How is this even possible if, from what I understand, rc.local gets run as root, therefore anything it executes should be run as root as well, and hence test.log should be created regardless?

startscript.sh:

#!/bin/sh
touch test.log
#... other stuff

rc.local snippet:

#!/bin/sh -e
#.. other stuff
sh /home/debian/startscript.sh
  • 2
    You can get permission denied even as root: try to write to a read-only filesystem, try to write to a special filesystem (e.g., /proc or /sys) in a not-permitted way, try to write to a network filesystem (server does permission checks, too), etc... You haven't really given us enough information to figure out which you're seeing. What is the current working directory of startscript.sh? – derobert Feb 6 at 18:41
  • 1
    To build off of user derobert, please include what permissions your script has, as well as the permissions of the working directory of the script or where the script is being run from. Is your / filesystem mounted as read only? Either touch is being run in a filesystem that is mounted as read-only or the script is being run before the filesystem has assigned the relevant options in fstab during the boot up process. The first option is far more likely than the latter I feel. – kemotep Feb 6 at 18:51
  • @derobert and @Engineer999: unless the #.. other stuff includes any explicit cd commands, then the current working directory of startscript.sh will be /, and so the file it's attempting to create will be /test.log. When you log in, your working directory is set to be your home directory (on modern systems, most likely by the PAM session module), but for start-up scripts, this convenience does not exist. – telcoM Feb 6 at 19:16
  • @derobert The current working directory for startscript.sh is /home/debian as shown in the rc.local file snippet – Engineer999 Feb 7 at 15:46
  • @kemotep The file system is not read-only. This is why I don't understand the problem. It should create the file . I also did sudo chmod 777 /home/debian to ensure everyone has permissions and it still complains – Engineer999 Feb 7 at 15:49
0

There are two problems with that approach. One, rc.local is executed by root, so all the files it creates will be owned by root unless you run chown on them. Second, it's a security hole in that root executes code which is potentially modifiable by a user without root privilege.

A better way is to use the @reboot time specifier with cron as an unprivileged user. If you already have a crontab file, edit it to add this line and then run the crontab <yourcrontab> command. Or use the crontab -e command to enter this line directly.

@reboot /home/debian/startscript.sh

Note that you don't need sh if the script is executable because it starts with #!/bin/sh.

  • 2
    Or one could write a native service definition for whatever one's service manager is, with appropriate user account settings. unix.stackexchange.com/a/471871/5132 – JdeBP Feb 6 at 19:56
  • I don't want a better way. I know Cron works ok for this. I'm just trying to understand why rc.local won't work – Engineer999 Feb 7 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.