21

I am writing a bash script to set up the all the software I need after installing Ubuntu. If the computer is a laptop, I want to install tlp for power management. If it is a desktop, tlp is not necessary.

Is there a command with which I can figure out whether the computer is a desktop or a laptop/tablet?

Perhaps, detecting the presence of a battery is the signature I should look for.

2 Answers2

31

I don't think checking the battery is a good solution: A desktop may have UPS that shows up as battery.

You can use the command:

$ hostnamectl chassis
laptop

The output will be

"desktop", "laptop", "convertible", "server", "tablet", "handset", "watch", "embedded", as well as the special chassis types "vm" and "container" for virtualized systems that lack an immediate physical chassis.

See man hostnamectl.


Another option would be to use:

$ sudo dmidecode -s chassis-type
Notebook

Note that in this case, the output may be something like "Laptop", "Notebook", "Portable", "Hand held" or "Sub Notebook" depending on the manufacturer's designation.


There is also a script named:

$ laptop-detect -v
We're a notebook (chassis_type is 10)

You can inspect its (/bin/laptop-detect) contents for a more detailed approach.

FedKad
  • 13,420
8

Checking on whether or not the system has a battery is not reliable - a UPS connected to the system may show up as a battery (which showed up when I checked).

So one way is to use dmidecode to get the chassis type of the system:

dmidecode --string chassis-type

But this command requires sudo. To avoid using sudo, you can print the contents of /sys/class/dmi/id/chassis_type, which will return the decimal value of the chassis type.

cat /sys/class/dmi/id/chassis_type

But when I checked, both of the commands gave output where my desktop was considered as "Rack Mount Chassis" (decimal value 23) and my laptop as "Notebook" (decimal value 10). So it might vary according to the system that we are handling.

That's when I used the hostnamectl command and got better values from the Chassis and Icon name:

hostnamectl status | grep Chassis | cut -f2 -d ":" | tr -d ' '

Since my desktop machine didn't have any Chassis option, I used the Icon name value:

hostnamectl status | grep "Icon name" | cut -f2 -d ":" | tr -d ' '

So on my laptop the result was "computer-laptop", on desktop it's just "computer" and on my server, it is "computer-vm". You can use these values to install the tlp on the system.

Ajay
  • 2,241