I have a script which simply echos current time and appends the output to a file. Below is the bash script.
#!/bin/bash
#Filename: /etc/init.d/bootlog
#Bash script to record a timestamp.
echo "Time: `date`" >> /home/foo/Documents/.boot_log
exit
I wanted to run this script every time my computer booted to runlevel 2, so I placed the above script in /etc/init.d/
directory with execute permission for root
.
$ ls -l /etc/init.d/bootlog
-rwx------ 1 root root 161 Jan 12 11:02 /etc/init.d/bootlog
$
Next, I added the required symlink as below.
$ cd /etc/rc2.d/
$ sudo ln -s /etc/init.d/bootlog S06bootlog
$ sudo ls -l /etc/rc2.d/S06bootlog
lrwxrwxrwx 1 root root 18 Jan 12 12:34 /etc/rc2.d/S06bootlog -> /etc/init.d/bootlog
$
After doing the above steps I was expecting the script to run in my next reboot. However the script is NOT running during boot. I can see that because the .boot_log
file is not created.
$ cd /home/foo/Documents/
$ sudo ls -l -a .boot_log
ls: cannot access .boot_log: No such file or directory
$
Is there any step I missed ? Or anything I did wrong ?
Yup, you did something wrong when looking for the file.
When you ran
cd /home/foo/Documents/
and thenls -l -a .boot_log
you were attempting to find that file in the/home/foo/Documents/
folder.However, your script is storing it in your home directory (
/home/foo/
), as per>> /home/foo/.boot_log
. Try runningls -la .boot_log
in/home/foo/
and the file should show up.