1

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:

  1. logout of my user.
  2. press CTRL + ALT + F2 in order to see tty2.
  3. Log in to my user and sudo service lightdm stop in order to disable GUI.
  4. 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:

  1. Why do they work now?
  2. 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)

  • /path_to_some_folder/ isn't mounted yet? Is /path_to_some_folder/ your encrypted $HOME? – waltinator Oct 5 '16 at 21:05
  • @waltinator That folder is mounted. It's not encrypted. (When the file-system mounting process is performed?) – Dor Oct 5 '16 at 21:14
  • Try typing the commands in by hand. Maybe an error message will be displayed. – WinEunuuchs2Unix Oct 5 '16 at 22:03
  • @WinEunuuchs2Unix Please see my update. – Dor Oct 6 '16 at 17:11
  • @waltinator Please see my update. – Dor Oct 6 '16 at 17:12
5

I understand now - Ubuntu's runlevel differ from the standards, see: http://upstart.ubuntu.com/cookbook/#runlevels

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 the rc system that preceded the van Smoorenburg rc 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 Smoorenburg init for a decade.

You're using Ubuntu 16.04. Don't begin with Version 6 UNIX rc or van Smoorenburg rc; be wary of beginning with upstart job files; forget about van Smoorenburg init; 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

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.