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
+F2
in order to seetty2
. - Log in to my user and
sudo service lightdm stop
in order to disable GUI. - Then
sudo init 3
in 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)
Best Answer
No, actually you don't understand.
You're working with a mental model of the operating system that has twice been superseded on Ubuntu. Upstart was introduced a fair few years ago; and the version that you are using now (16.04) has superseded even that.
You are using Ubuntu 16.04, a systemd operating system. Run levels are "obsolete" according to the systemd doco. You can forget about them. The Upstart Cookbook and the manual pages for upstart's
init
do not apply (unless you have explicitly switched back to upstart).You don't need to mess around with
/etc/rc.local
. That's from therc
system that preceded the van Smoorenburgrc
system that you are erroneously thinking you are using now. It's a mechanism that has been superseded three times./etc/inittab
is a thing of the past, too. Ubuntu hasn't had van Smoorenburginit
for a decade.You're using Ubuntu 16.04. Don't begin with Version 6 UNIX
rc
or van Smoorenburgrc
; be wary of beginning with upstart job files; forget about van Smoorenburginit
; and forget about run levels. Make a systemd service unit, and learn about targets.(Or install daemontools, or daemontools-encore, or freedt, or runit, or nosh, or perp, or s6, and learn about daemontools-style services.)
Further reading
/etc/inittab
is a thing of the past.. Frequently Given Answers.