Detect Linux Runlevel

Lew Pitcher lew.pitcher at digitalfreehold.ca
Mon Dec 5 17:45:57 EST 2016


On Monday December 5 2016 17:24, in comp.lang.python, "Chris Angelico"
<rosuav at gmail.com> wrote:

> On Tue, Dec 6, 2016 at 8:38 AM, Lew Pitcher
> <lew.pitcher at digitalfreehold.ca> wrote:
>> The OP asked for the runlevel, not the systemd target.
> 
> Runlevels don't exist in systemd. And systemd targets don't exist in
> Upstart. The question "what runlevel are we in" does not make sense
> unless you're using an init system that works on the basis of
> runlevels (eg sysvinit).

I repeat: The OP asked for the runlevel, not the systemd target.

That should tell you that an answer involving systemd "does not make sense".

To the OP: as others have said, the file
  /var/run/utmp
contains various records, including the RUN_LVL (runlevel) record. You can
find some documentation in utmp(3), including a record layout, and an values
list.

I don't know that python includes a standard or builtin method to parse the
utmp file; to retrieve the runlevel, you may have to code your own access
routine as part of your python code, or resort to invoking an external
program like who(1).

HTH
-- 
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request




More information about the Python-list mailing list