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
/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 ofstartscript.sh
? – derobert Feb 06 '19 at 18:41/
filesystem mounted as read only? Eithertouch
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 06 '19 at 18:51#.. other stuff
includes any explicitcd
commands, then the current working directory ofstartscript.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 06 '19 at 19:16sh /home/debian/startscript.sh
does not change the current working directory. (cd /home/debian/
would, but just executing a script out of it does not.) – derobert Feb 07 '19 at 20:15cd /home/debian && ./startscript.sh
would fix that. (Though do pay attention the warnings Ken Jackson has given you.) – derobert Feb 11 '19 at 18:32