To use Top Command In Batch Mode - command

How do we use Top command in batch mode to sort based on Swap memory and Virtual memory ?
Like we have "top -m" option to sort per Memory usage.

Please use -o with field name.
top -b -o %MEM

Related

How to make handbrake use the cpu with less intensity?

I've recently began using HandBrake to process some videos I downloaded to make them lighter. I built a small python GUI program to automate the processing, making use of the CLI version. What I am doing is generating the command according to the video and executing it with os.system. Something like this:
import os
def process(args):
#some algorithm to generate cmd using args
cmd = "handbrakecli -i raw_video.mp4 -o video.mp4 -O -e x264" #example command
os.system(cmd)
os.remove("raw_video.mp4")
The code works perfectly, but the problem is the overuse of my CPU. Usually, this takes 100% of CPU usage during considerable amount of time. I use the program CoreTemp to keep track of my processor temperature and, usually, it hits 78 °C.
I tried using BES (Battle Encoder Shirase) by saving the cmd command into a batch file called exec.bat and doing os.system("BES_1.7.7\BES.exe -J -m exec.exe 20"), but this simply does nothing.
Speed isn't important at all. Even if it takes longer, I just want to use less of my CPU, something around 50% would be great. Any idea on how I could do so?
In Handbrake you can pass advanced parameters so you only use a certain amount of CPU threads.
You can use threads, view the Handbrake CLI Documentation
When using threads you can specify any number of CPU processors to use. The default is auto.
The -x parameter stands for Advanced settings in the GUI of Handbrake, that is where threads will go.
The below tells Handbrake to only use one CPU thread for the Advanced setting:
-x threads=1
You can also use the veryslow for the --encoder-preset setting to help the CPU load.
--encoder-preset=veryslow
I actually prefer using the --encoder-preset=veryslow preset since I see an overall better quality in the encode.
And both together:
--encoder-preset=veryslow -x threads=1
So formatted with your cmd variable:
cmd = "handbrakecli -i raw_video.mp4 -o video.mp4 -O -e x264 --encoder-preset=veryslow -x threads=1" #example command
See if that helps.
One easy way in Linux is to use taskset. You can use the terminal or make a custom shortcut/command.
For example, my CPU has 8 threads but I only want to use 6 for Handbrake.
Just start the program with taskset -c 2,3,4,5,6,7 handbrake, this way the threads 0 and 1 will be free to another task/process and the program will run on threads 2,3,4,5,6,7.
In Windows you can change the Target of the shortcut or use on cmd:
C:\Windows\System32\cmd.exe /C start "" /affinity FC "C:\Program Files\HandBrake\HandBrake.exe""
As far as I understand It reads the value backwards for each four bits, it means the first four bits in Hexadecimal are for threads 7-4 (1111) and the second four bits in Hexadecimal are for threads 3-0 (1100). In my case I have a 8 threads CPU and leaving free theads 1 and 0 (see image below).

how to display the CPU percentage usage by each process in cmd

i want to a cmd windows command to display the all the processes and the cpu percentage for each process.
is there a command which give me this result?
can you help me please?
thank you
Try pslist from the SysInternals-powered pstools.
You will need to download them from that link and put the tools in your cmd directory (or chdir to wherever they are).
Use -s to see the CPU usage of each process.
Perfmon can use a wildcard to get the CPU usage for each running process. It also has the text interface typeperf which spits the results out to the console.
This command will produce a one-line CSV output of the current running process CPU usage:
typeperf "\process(*)\% processor time" -sc 1
The PID is missing from this report. If you need, you can add the PID for each of the processes as a separate counter to log, then match up the names:
typeperf "\process(*)\% processor time" "\process(*)\id process" -sc 1

Pane Title in Tmux

On my local machine I've got 3 node.js instances running simultaneously. Each has it's own pane in a tmux window called "servers". The problem is that it's not that easy to figure out which node is running in which pane, 'cause their logs are similar.
What I need is a title for every pane. As I got, tmux itself doesn't have the feature: it has only titles for windows and not for panes. Launching a separate tmux session inside every pane for every node.js instance looks like an overkill.
So is there some small program that launches a command, wrapping its output with a specified status bar?
This functionality has been added to tmux in this commit. It is not in version 2.2, but it looks like it will be in 2.3.
To enable it:
tmux set -g pane-border-status top
or if you prefer:
tmux set -g pane-border-status bottom
To set a custom text as your pane border status line you can make use of pane-border-format, e.g. like so:
tmux set -g pane-border-format "#{pane_index} #{pane_current_command}"
tmux does support per-pane titles, but it does not provide a per-pane location to display these titles.
You can set a pane’s title with the escape sequence ESC ]2; … ESC \ (e.g. see the section called Names and Titles in the tmux manpage). You could do this from the shell like this:
printf '\033]2;%s\033\\' 'title goes here'
Each pane’s title defaults to the system’s hostname. By default the active pane’s title is displayed on the right side of the tmux status line (the default global value of the session variable status-right is "#22T" %H:%M %d-%b-%y, which shows 22 characters of the pane’s title, the time, and the date).
So, as long as you are satisfied with being able to see the active pane’s title (i.e. willing to switch panes to see the title of an inactive pane), you can get by with the default functionality. Just send the appropriate title-setting escape sequence before starting the main command for each pane.
If you absolutely need a dedicated line to display some per-pane information, then nested tmux sessions may not be as much (unnecessary) “overkill” as you might first think.
In the general case, to provide an inviolate status line on some given terminal, you will need a full terminal (re)emulator that sits between the original terminal and a new terminal (one with one fewer lines). Such (re)emulation is needed to translate control sequences sent to the inner terminal and translate them for the original terminal. For example, to maintain a status line at the bottom of the outer terminal, the command
Move to the last line.
sent to the inner terminal must be become
Move to the next to last line.
when translated for and sent to the outer terminal. Likewise, an LF sent to the inner terminal must become
If the cursor is on the next to last line, then scroll this line and all the lines above it up one line, to provide a clear next-to-last line (protecting the status line on the last line).
Otherwise, send an LF.
in the outer terminal.
Programs like tmux and screen are just such terminal re-emulators. Sure, there is a lot of other functionality wrapped around the terminal emulator, but you would need a large chunk of terminal emulation code just to provide a reliable status line.
There is, however, a light-weight solution as long as
your programs (Node.js instances) have limited terminal interactions with the panes in which they are running (i.e. no cursor positioning), and
you do not resize the panes while your programs are running.
Like many terminal emulators, tmux supports a “set scrolling region” terminal control command in its panes. You could use this command to limit the scrolling region to the top (or bottom) N-1 lines of the terminal and write some sort of instance-identifying text into the non-scrolling line.
The restrictions (no cursor movement commands allowed, no resizing) are required because the program that is generating the output (e.g. a Node.js instance) has no idea that scrolling has been limited to a particular region. If the output-generating program happened to move the cursor outside of the scrolling region, then the output might become garbled. Likewise, the terminal emulator probably automatically resets the scrolling region when the terminal is resized (so the “non-scrolling line” will probably end up scrolling away).
I wrote a script that uses tput to generate the appropriate control sequences, write into the non-scrolling line, and run a program after moving the cursor into the scrolling region:
#!/bin/sh
# usage: no_scroll_line top|bottom 'non-scrolling line content' command to run with args
#
# Set up a non-scrolling line at the top (or the bottom) of the
# terminal, write the given text into it, then (in the scrolling
# region) run the given command with its arguments. When the
# command has finished, pause with a prompt and reset the
# scrolling region.
get_size() {
set -- $(stty size)
LINES=$1
COLUMNS=$2
}
set_nonscrolling_line() {
get_size
case "$1" in
t|to|top)
non_scroll_line=0
first_scrolling_line=1
scroll_region="1 $(($LINES - 1))"
;;
b|bo|bot|bott|botto|bottom)
first_scrolling_line=0
scroll_region="0 $(($LINES - 2))"
non_scroll_line="$(($LINES - 1))"
;;
*)
echo 'error: first argument must be "top" or "bottom"'
exit 1
;;
esac
clear
tput csr $scroll_region
tput cup "$non_scroll_line" 0
printf %s "$2"
tput cup "$first_scrolling_line" 0
}
reset_scrolling() {
get_size
clear
tput csr 0 $(($LINES - 1))
}
# Set up the scrolling region and write into the non-scrolling line
set_nonscrolling_line "$1" "$2"
shift 2
# Run something that writes into the scolling region
"$#"
ec=$?
# Reset the scrolling region
printf %s 'Press ENTER to reset scrolling (will clear screen)'
read a_line
reset_scrolling
exit "$ec"
You might use it like this:
tmux split-window '/path/to/no_scroll_line bottom "Node instance foo" node foo.js'
tmux split-window '/path/to/no_scroll_line bottom "Node instance bar" node bar.js'
tmux split-window '/path/to/no_scroll_line bottom "Node instance quux" node quux.js'
The script should also work outside of tmux as long as the terminal supports and publishes its csr and cup terminfo capabilities.
Since tmux 2.6 you can do:
$ tmux select-pane -t {pane} -T {title}
# Examples:
$ tmux select-pane -T title1 # Change title of current pane
$ tmux select-pane -t 1 -T title2 # Change title of pane 1 in current window
$ tmux select-pane -t 2.1 -T title3 # Change title of pane 1 in window 2
You can see title per pane in a status bar with:
$ tmux set pane-border-status bottom # For current window
$ tmux set -g pane-border-status bottom # For all windows
Disable status bar with:
$ tmux set pane-border-status off # For current window
$ tmux set -g pane-border-status off # For all windows
I am using tmux version 2.3, I think border style is not supported in previous versions.
this is what worked for me:
For each pane set the title:
printf '\033]2;My Pane Title\033\\'
Then:
tmux set -g pane-border-format "#{pane_index} #T"
A gif is worth a thousand words. (source)
tmux-xpanes is a tmux-based terminal divider, which supports
displaying title for each pane through the newly added -t option.
I'm working on the pane status bar for tmux - ticket.
My development branch can be found here on github:
https://github.com/jonathanslenders/tmux
Right now, this already adds a working rename-pane "title" command. There are still some bugs, and the API will improve. The idea is to create a status bar per pane, which can have some formatting, like the session status bar. Like the rest of tmux, everything should become scriptable, and customizable. When finished and stable, it will probably be included in the official tmux.
Yes there is such a command: tmux. Give your session a name and it will be displayed in an inner status bar:
TMUX=0 tmux new-session -s my-new-session
TL;DR
Append following configs to your tmux config file (in my case is ~/.tmux.conf.local)
# dispaly pane_current_path as the pane title
set -g pane-border-status top
set -g pane-border-format "#{pane_index} #{pane_current_path}"
then run:
tmux source-file ~/.tmux.con
enjoy it
Thanks to https://stackoverflow.com/a/37602055/5121972
This is not helpful in the short-term, but there is a feature request for per-pane titles in tmux: http://sourceforge.net/tracker/?func=detail&aid=3495916&group_id=200378&atid=973265#
In the meantime, as others mentioned, nesting tmux works decently.
Adding these three lines inside .tmux.conf worked and doesn't intervene with the pane_title variable managed by tmux
set -g pane-border-status top
set -g pane-border-format "#[fg=black, bg=green] #{pane_index} #{#custom_pane_title}"
bind < command-prompt -p "New Title: " -I "#{#custom_pane_title}" "set-option -p #custom_pane_title '%%'"
After adding these lines, source the .tmux.conf to reflect the changes
tmux source-file ~/.tmux.conf
Inside tmux pane, press Ctrl+B <, enter the title of your choice, and pane title will be set.
The rationale to use a different variable
Pane title can be set from multiple sources, and I wanted to avoid any interference with it.
tmux select-pane -T title1
using escape sequence characters `printf '\033]2;%s\033\' 'title goes here
ref: https://gist.github.com/ethagnawl/db27bba3c4cccdc30ade2a0c54f49723
All existing answers don't mention how to actually change the default title, but the solution is hidden over at https://unix.stackexchange.com/a/564690/28354, and, for example, on Android Termux tmux, you can change the default title of "localhost" to the model name instead, like so, from within a zsh shell:
tmux set-hook -g after-split-window "select-pane -T \"$(getprop ro.product.model)\""
tmux set-hook -g after-new-window "select-pane -T \"$(getprop ro.product.model)\""
tmux set-hook -g after-new-session "select-pane -T \"$(getprop ro.product.model)\""
This will change the "localhost" from bottom-right status bar next to the clock, because pane_title is what's used, which, in turn, defaults to the hostname:
% tmux show-options -g status-right
status-right "#{?window_bigger,[#{window_offset_x}#,#{window_offset_y}] ,}\"#{=21:pane_title}\" %H:%M %d-%b-%y"
[0] 0:zsh 1:zsh 2:zsh- 3:zsh* "Pixel 2 XL" 22:55 26-Sep-21
Additionally, it can be made to be displayed at the top of each pane, too:
tmux set -g pane-border-status top
The format is controlled by pane-border-format, and it defaults as follows:
% tmux show-options -g pane-border-format
pane-border-format "#{?pane_active,#[reverse],}#{pane_index}#[default] \"#{pane_title}\""

How can I modify command line in Ubuntu 10.04?

I am using a vpn service from certain server. I was given with a root account, and when I connect with a root account, the command line looks like below.
root#xa9g82:/etc/#
Then I used useradd to add an account called 'temp'
When I connected to the server with temp, then the command line only has a single character.
$
The user information is not shown, neither the path. Also, note that, in root's command line I can use tab to automatically complete the filename, however 'temp's command line inserts tab space, when I press tab. It is very inconvenient.
I am using Ubuntu 10.04. How can I resolve this issue?
I usually edit ~/.bashrc. Being root, you might want to change the system-wide preferences, at /etc/bash.bashrc. Personally, I changed some lines in ~/.bashrc to look like:-
# If this is an xterm set the title to user#host:dir
case "$TERM" in
xterm*|rxvt*)
## PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u#\h: \w\a\]$PS1" # default
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\h: \W\a\]$PS1" # How I like it
;;
*)
;;
esac
use prompt to set the prompt.... (man prompt...)
it depends on what shell you run each one has it's own tricks, but you can make it looks as you wish.
BASH
TCSH
It is likely that the default shell for root is set to /bin/sh, which does not provide many of the features that you may used to if you use a shell like bash. To check if this is the case, run the following command:
cat /etc/passwd | grep ^root
The last component of the line that this command outputs will be your shell (which, as stated previously, I'm guessing is /bin/sh). If this is not the shell you want (it probably isn't), then edit /etc/passwd (using nano or whatever editor you're most comfortable with) and change your shell to something more palatable, like /bin/bash. After doing this, you'll need to log out and then log back in.

How do I get the "Command Buffer" in Solaris 10?

When working on a linx CShell u get the option to press the up / down arrows to select the last command/s typed or the Command Buffer. This even works on Windows.
However this is not functional when working on Solaris, to which i recently switched. I am guessing that the shell is also a CShell.
Please tell me what key combination is required to have this feature on Solaris ?
The default shell in Solaris has command history, but you can also use Bash instead, it's more user friendly. Just type 'bash' (no quotes) at the command line. You can also edit /etc/passwd to make bash your default shell.
The "official" default shell for Solaris is actually sh, the original Bourne shell (see Chapter 10 of the Advanced User Guide for Solaris for more info). If you'd like to change it to csh or tcsh—and you're not root (it's generally considered bad practice to use anything but sh as root's default)—just issue passwd -e /path/to/shell_of_your_choice <loginname>. I'm guessing this would probably look like passwd -e /bin/csh <loginname>, but you'd probably want to make sure it exists, first.
It may be that it's the Korn shell in which case try <ESC>k.
bash at least will allow you to switch modes with "set -o vi" or "set -o emacs".
Maybe you can use the !! command, to repeat the previous one.
Use "echo $SHELL" to see what your login shell is. If it's ksh or bash, try "set -o emacs". If that works, you'll be able to use ^P to go back a command. ^R lets you search for a command, ^F and ^B to move around within the command.
If you can´t change your default shell, or you just want to try out one that works, you can kick off any other shell from your command line. I recommend you tcsh, which will have good command line editing and history using the arrow keys. Type /bin/tcsh at your prompt to try it out. You can use the earlier responses to change your default shell if you like tcsh. Make sure your have the following in your $HOME/.cshrc file:
set filec
set history=1000 # or some other large number
set autologout=0 # if you are logging in remotely under your account.
I hope this helps.
You enable history temporarily if you use BASH by typing
HISTSIZE=1000
which will enable up and down keys and store 1000 commands. After termal disconnetion all history will be gone.
This works on solaris 10.
For permanent solution add these lines to ~/.bashrc
HISTSIZE=1000
HISTFILESIZE=1000