15 Eylül 2019 Pazar

strace komutu

Giriş
Açıklaması şöyle.
strace is a diagnostic, debugging and instructional userspace utility for Linux. It is used to monitor and tamper with interactions between processes and the Linux kernel, which include system calls, signal deliveries, and changes of process state.
Açıklaması şöyle.
strace only shows when the process enters (and then leaves) the kernel due to a system call. Or when a unix signal is delivered. However there are other types of interrupts which strace does not show at all. So these include

- Page faults.
- The timer interrupt. This is used to switch to a different process, when the current one has exhausted its allocated time slice on the CPU.
-c seçeneği
Açıklaması şöyle.
-c
Count time, calls, and errors for each system call and report a summary on program exit. On Linux, this attempts to show system time (CPU time spent running in the kernel) independent of wall clock time. If -c is used with -f or -F (below), only aggregate totals for all traced processes are kept.
Örnek
Şöyle yaparız.
strace -c -p 3569 # 3569 is PID
strace: Process 3569 attached
^Cstrace: Process 3569 detached
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
99.73    0.016000           8      1971           poll
0.16    0.000025           0       509        75 futex
0.06    0.000010           0      1985      1966 recvmsg
0.06    0.000009           0      2336           mprotect
0.00    0.000000           0       478           read
0.00    0.000000           0        13           write
0.00    0.000000           0        29           mmap
0.00    0.000000           0         9           munmap
0.00    0.000000           0        18           writev
0.00    0.000000           0       351           madvise
0.00    0.000000           0         1           restart_syscall
------ ----------- ----------- --------- --------- ----------------
100.00    0.016044                  7700      2041 total
-e seçeneği
Beliritlen sistem çağrısısını gösterir. 
Örnek
futex sistem çağrısı için şöyle yaparız
$ strace -e futex ./sleepgranularity 
futex(0x7fff800b3ad8, FUTEX_WAIT_PRIVATE, 0, {tv_sec=0, tv_nsec=4084}) = -1 ETIMEDOUT
(Connection timed out)
Örnek
open sistem çağrısı için şöyle yaparız.
strace -eopen someprogram
Örnek
open,read,write sistem çağrıları için şöyle yaparız.
some_command | strace -o less.trace -e open,read,write less
Örnek
openat,write,fork,vfork,clone,execve sistem çağrıları için şöyle yaparız
$ strace -e trace=openat,read,write,fork,vfork,clone,execve -p 2883 2> bash.strace
Çıktı olarak şunu alırız.
strace: Process 2883 attached
read(0, "l", 1)                         = 1
write(2, "l", 1)                        = 1
read(0, "s", 1)                         = 1
write(2, "s", 1)                        = 1
read(0, "\r", 1)                        = 1
write(2, "\n", 1)                       = 1
Örnek - delay
Şöyle yaparız
$ strace -e inject=unlink,unlinkat,rmdir:delay_enter=5s 
         -e unlink,unlinkat,rmdir 
          rm -rf /tmp/tmp.HudBncQ4Ni
unlinkat(4, "test", 0^Z
zsh: suspended  strace -e inject=unlink,unlinkat,rmdir:delay_enter=10s -e  rm -rf
Örnek - signal göndermek
Şöyle yaparız
strace -e inject=unlink,unlinkat,rmdir:signal=STOP
Örnek - sonuç dönmek
Şöyle yaparız
strace -e inject=unlink,unlinkat,rmdir:retval=0 -e unlink,unlinkat,rmdir ...

veya

zmodload zsh/system
strace -e inject=unlink,unlinkat,rmdir:error=EACCES -e unlink,unlinkat,rmdir ...

-ff
-ff seçeneği
-o ile kullanılırsa dosya ismi şu formatta olur.
uygulama.pid
-o seçeneği
Çıktıyı dosyaya kaydetmek için kullanılır.
Örnek
Şöyle yaparız.
strace -ff -o TRACE java MyApp
-ttt seçeneği
Timestamp'i mikrosaniye olarak gösterir. Şöyle yaparız.
strace -ttt -T -C -w foo
-T seçeneği
Sistem çağrısı içinde harcanan süreyi gösterir. Açıklaması şöyle.
time is shown in seconds, with microseconds (calculated from the nanosecond value) after the decimal point.
Örnek
Şöyle yaparız.
strace -T  sleep 2
Çıktı olarak şunu alırız.
nanosleep({tv_sec=2, tv_nsec=0}, NULL)  = 0 <2.000230>

12 Eylül 2019 Perşembe

parted komutu

Örnek
Şöyle yaparız.
root@irb00a ~]# parted /dev/sda1
GNU Parted 3.2
Using /dev/sda1
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: Unknown (unknown)
Disk /dev/sda1: 12.0TB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags:

Number  Start  End     Size    File system  Flags
 1      0.00B  12.0TB  12.0TB  xfs

locale komutu

Örnek
Değerleri görmek için locale komutunu kullanarak şöyle yaparız.
$ locale
LANG=en_GB.UTF-8
LANGUAGE=
LC_CTYPE="en_GB.UTF-8"
LC_NUMERIC="en_GB.UTF-8"
LC_TIME="en_GB.UTF-8"
LC_COLLATE="en_GB.UTF-8"
LC_MONETARY="en_GB.UTF-8"
LC_MESSAGES="en_GB.UTF-8"
LC_PAPER="en_GB.UTF-8"
LC_NAME="en_GB.UTF-8"
LC_ADDRESS="en_GB.UTF-8"
LC_TELEPHONE="en_GB.UTF-8"
LC_MEASUREMENT="en_GB.UTF-8"
LC_IDENTIFICATION="en_GB.UTF-8"
LC_ALL=
Örnek
Şöyle yaparız.
$ locale
LANG="en_GB.UTF-8"
LC_COLLATE="en_GB.UTF-8"
LC_CTYPE="en_GB.UTF-8"
LC_MESSAGES="en_GB.UTF-8"
LC_MONETARY="en_GB.UTF-8"
LC_NUMERIC="en_GB.UTF-8"
LC_TIME="en_GB.UTF-8"
LC_ALL=
decimal_point seçeneği
Şöyle yaparız.
locale decimal_point
thousands_sep seçeneği
Şöyle yaparız.
locale thousands_sep


getent komutu

Giriş
Açıklaması şöyle. Bu uygulama POSIX'te tanımlı değil ancak çoğu sistemde mevcut.
Many systems have a getent command to list or query the content of the Name Service databases like passwd, group, services, protocols...
hosts seçeneği
Açıklaması şöyle.
You can check to see which IP NSS resolves a hostname to using getent.
Şöyle yaparız.
getent hosts somename
passwd seçeneği
Örnek - Home dizin
Home dizinleri almak için şöyle yaparız.
getent passwd | cut -d: -f6
Örnek - Kullanıcı ismi
Kullanıcı isimlerini almak için şöyle yaparız.
getent passwd | cut -d: -f1
Kullanıcı ismini almak için id komutu da kullanılabilir.

9 Eylül 2019 Pazartesi

which komutu - type komutunu kullanın

Giriş
Not : Bu komut yerine type komutu tercih edilmeli

Açıklaması şöyle.
which searches for binaries in the $PATH, a.k.a. command-line tools. User applications are not such tools and not available in the command line.
Windows'taki "where" komutuna benzer. 

Örnek
Şöyle yaparız.
$ which python
/anaconda3/bin/python
Örnek
Şöyle yaparız.
$ which uname
/bin/uname
Örnek
Şöyle yaparız.
$ which ping
/usr/bin/ping 
Örnek - İki Parameter Birden
Şöyle yaparız.
$ which node postfix
/root/.nvm/versions/node/v14.15.1/bin/node
/usr/sbin/postfix

















6 Eylül 2019 Cuma

cut komutu

Giriş
Kesilecek yeri belirtmek için
1. -c veya  -f kullanılır
2. -f kullanıyorsak ayracı belirtmek için gerekiyorsa -d kullanılır.
Açıklaması şöyle.
cut uses -d or --delimiter to specify the field delimiter and -f or --fields to specify fields.
UTF-8
cut komutu sadece byte bilir. UTF-8 ve diğer multi-byte encoding'leri bilmez. Açıklaması şöyle
The cut command in Ubuntu is not multi-byte character aware. Characters are the same as bytes for this version of the cut command.
Örnek
UTF-8 ile çalışmadığını görmek için şöyle yaparız
$ echo £ |cut -c 1
�
-c seçeneği
Select only these characters anlamına gelir. Belirtilen karakter aralığını döndürür
Örnek
UTF-8 ile çalışmak için şöyle yaparız
$ echo £ |cut -c 1-2
£
-d seçeneği
Kullanılacak ayracı belirtir.
Örnek
Şöyle yaparız. Çıktı stdout'a gönderilir.
cut -d: -f2-
-f seçeneği
Döndürülecek sütunları belirtir.
Örnek
Eğer döndürülecek sütun yoksa newline döndürülür. Görmek için şöyle yaparız.
echo "$versionfull" | cut -d. -f3 | od -c
0000000  \n
0000001
Örnek
İlk 3 alan için şöyle yaparız.
#!/bin/bash
FILES=/path/to/*
for f in $FILES
do
    # Do something for each file. In our case, just echo the first three fields:
    cut -f1-3 < "$f"
done