Why is the process name shortened when I get the top command from a self-produced script that I started with systemctl?

Asked 2 years ago, Updated 2 years ago, 128 views

In CentOS 8, we created a shell script that records the results of the top command in a log file every 10 seconds.

#!/bin/sh

while true
do
  top-b-n 1 | head-30>>/var/log/process-logger.log
  sleep10
done

This script has been set to start the service on systemctl.The service configuration file is as follows:

 [Unit]
Description= Process Logger
After=network.target

Service
Type = simple
ExecStart=/home/username/process-logger/main
WorkingDirectory=/home/username/process-logger
KillMode=process
Restart=always

Install
WantedBy=multi-user.target

As a result, the output process name of the top command is shortened and recorded in the log file.
For example, the original content was:

6 root 0-2000 I 0.00:00.00 kworker/0:0H-kblockd
    8 root 0-2000 I 0.0000.00 mm_percpu_wq

If you start with systemctl, the process name is only halfway and the rest is the "+" symbol as follows:

6 root 0-2000 I 0.00:00.00 kworker/0+
    8 root 0-2000 I 0.0000.00 mm_percpu+

There is no problem if you run the script manually on the terminal.
Running from systemctl seems to shorten it.

Why is this?
Would it be possible to make everything come out without shortening it?

Thank you for your cooperation.

linux centos shellscript systemd

2022-09-30 14:25

1 Answers

I'm a questioner, but I solved it after receiving your comment.

  • Resolved by adding the option -w512 to the top command. (Process names are no longer shortened.)
  • The -w section of the mantop states:

Note: Without the use of this command-line option, output width is always based on the terminal at which top was invoked either or not in Batch mode.

  • Perhaps the top command is implemented to adjust the output according to the width of the invoked terminal window, but if the startup source is systemctl or cron, the minimum width is adopted because there is no terminal.

Thank you @metropolis and @cubick.


2022-09-30 14:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.