I wish to create a script that will run on startup of runlevel 3 only once.
Therefore now I perform tests to see if the execution really works.
I tried to create a file at /etc/init.d/myscript that contains:
#!/bin/bash
OUT="/dev/tty1"
echo -e "Hello world!" > $OUT
ps auxf > /path_to_some_folder/_script_ps_auxf.txt
runlevel > /path_to_some_folder/_runlevel.txt
id -u > /path_to_some_folder/id.txt
pwd > /path_to_some_folder/pwd.txt
That file has execution bit enabled for all users (ugo).
Also I created a symbolic link to /etc/init.d/myscript at /etc/rc3.d/S99myscript.
Then I:
- logout of my user.
- press
CTRL+ALT+F2in order to seetty2. - Log in to my user and
sudo service lightdm stopin order to disable GUI. - Then
sudo init 3in order to be at runlevel 3.
But the files at /path_to_some_folder/ don't exist after the init 3. Why?
I tried also several other variations of the above - including usage of update-rc.d.
In addition I tried to edit /etc/rc.local such that the code in /etc/init.d/myscript is embedded to /etc/rc.local prior to the exit 0 line, but still nothing happens (no file is created).
Update 6 Oct , 18:00 UTC:
For some reason now I can see all the files in the directory /path_to_some_folder/:
(I added to all files an underscore _ in order to distinguish them better from other files)
$ ls /path_to_some_folder/_*
/path_to_some_folder/_id.txt /path_to_some_folder/_pwd.txt /path_to_some_folder/_runlevel.txt /path_to_some_folder/_script_ps_auxf.txt
By inspection of their time, they're all created when the system is powered on.
By inspection of the file /path_to_some_folder/_script_ps_auxf.txt, I noticed that the init process was started:
/bin/bash /etc/init.d/myscript start
Therefore I understand that the init scripts work.
Indeed, when I press CTRL + ALT + F1 in order to see tty1, I notice the text "Hello world!" at the top (prior to the login prompt).
The only problem is to realize:
- Why do they work now?
Why the script is executed on the default runlevel of Ubuntu 16.04 ?
They should be executed only at runlevel 3, see:$ ls -1 /etc/rc*.d/*myscript* /etc/rc3.d/S99myscript
Answering question #1 myself:
I suspect that a restart was required in order for the init scripts to work.
About question #2:
The file /etc/inittab doesn't exists, therefore it can't determine the runlevel:
$ ls /etc/inittab
ls: cannot access '/etc/inittab': No such file or directory
The default runlevel, according to /etc/init/rc-sysinit.conf, is 2:
$ grep -m 1 DEFAULT_RUNLEVEL= /etc/init/rc-sysinit.conf
env DEFAULT_RUNLEVEL=2
There are no kernel command lines parameters according to the file /etc/default/grub:
$ less /etc/default/grub | grep GRUB_CMDLINE_LINUX=
GRUB_CMDLINE_LINUX=""
When I power on my PC and login to my user, I notice that my runlevel is 5:
$ runlevel
N 5
My script was supposed to run at runlevel 3 only.
Any ideas what's going on?
Update 6 Oct, 18:40 UTC:
I understand now - Ubuntu's runlevel differ from the standards, see:
http://upstart.ubuntu.com/cookbook/#runlevels
This means that runlevels 2 to 5 are all the same:
Graphical multi-user plus networking (DEFAULT)