Menggunakan Command Tail di terminal Linux
Perintah tail di Linux akan menampilkan bagian akhir dari sebuah file. Tail sangat powerful jika digunakan untuk membaca Log file. Karena tail dapat membaca file secara realtime, dan menampilkan setiap perubahan yang terjadi pada file.
Cara menggunakan tail,
tail namafile.ext
Sebagai contoh penggunaan tail secara sederhana
-n X | Untuk menampilkan tail dengan sejumlah X baris terakhir |
-X | Untuk menampilkan tail dengan sejumlah X baris terakhir |
-n | Untuk menampilkan tail dari beberapa file sekaligus |
-f | Untuk mentrack file dan menampilkan secara realtime |
Contoh
Penggunaan tail dengan menampilkan sejumlah baris tertentu. Sebagai contoh dari 2 baris terakhir
1 2 3 4 | [root@ip-xxxxxxx log]# tail -n2 awslogs.log 2022-08-04 10:10:40,700 - cwlogs.push.publisher - INFO - 2015 - Thread-3 - Log group: CMD-LOG, log stream: STG-COMM-DEP-A, queue size: 0, Publish batch: {'skipped_events_count': 0, 'first_event': {'timestamp': 1659582635000, 'start_position': 56018L, 'end_position': 56126L}, 'fallback_events_count': 0, 'last_event': {'timestamp': 1659582636000, 'start_position': 56342L, 'end_position': 56447L}, 'source_id': '1c78100b4e0442a9a293f1e5adc73885', 'num_of_events': 4, 'batch_size_in_bytes': 529} 2022-08-04 10:10:45,735 - cwlogs.push.publisher - INFO - 2015 - Thread-3 - Log group: CMD-LOG, log stream: STG-COMM-DEP-A, queue size: 0, Publish batch: {'skipped_events_count': 0, 'first_event': {'timestamp': 1659582639000, 'start_position': 56447L, 'end_position': 56550L}, 'fallback_events_count': 0, 'last_event': {'timestamp': 1659582639000, 'start_position': 56447L, 'end_position': 56550L}, 'source_id': '1c78100b4e0442a9a293f1e5adc73885', 'num_of_events': 1, 'batch_size_in_bytes': 128} [root@ip-xxxxxxx log]# |
1 2 3 4 | [root@ip-xxxxxxx log]# tail -2 awslogs.log 2022-08-04 10:10:40,700 - cwlogs.push.publisher - INFO - 2015 - Thread-3 - Log group: CMD-LOG, log stream: STG-COMM-DEP-A, queue size: 0, Publish batch: {'skipped_events_count': 0, 'first_event': {'timestamp': 1659582635000, 'start_position': 56018L, 'end_position': 56126L}, 'fallback_events_count': 0, 'last_event': {'timestamp': 1659582636000, 'start_position': 56342L, 'end_position': 56447L}, 'source_id': '1c78100b4e0442a9a293f1e5adc73885', 'num_of_events': 4, 'batch_size_in_bytes': 529} 2022-08-04 10:10:45,735 - cwlogs.push.publisher - INFO - 2015 - Thread-3 - Log group: CMD-LOG, log stream: STG-COMM-DEP-A, queue size: 0, Publish batch: {'skipped_events_count': 0, 'first_event': {'timestamp': 1659582639000, 'start_position': 56447L, 'end_position': 56550L}, 'fallback_events_count': 0, 'last_event': {'timestamp': 1659582639000, 'start_position': 56447L, 'end_position': 56550L}, 'source_id': '1c78100b4e0442a9a293f1e5adc73885', 'num_of_events': 1, 'batch_size_in_bytes': 128} [root@ip-xxxxxxx log]# |
Tail dengan beberapa file sekaligus
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [root@ip-xxxxxx log]# tail -n 4 cron-20220801 cron-20220724 ==> cron-20220801 <== Aug 1 07:14:01 ip-10-20-1-182 CROND[3564]: (root) CMD (/opt/aws-scripts-mon/mon-put-instance-data.pl --disk-space-avail --disk-path=/) Aug 1 07:14:01 ip-10-20-1-182 CROND[3565]: (root) CMD (/usr/sbin/awslogs-nanny > /dev/null 2>&1) Aug 1 07:15:01 ip-10-20-1-182 anacron[3106]: Job `cron.daily' started Aug 1 07:15:01 ip-10-20-1-182 run-parts(/etc/cron.daily)[3593]: starting logrotate ==> cron-20220724 <== Jul 24 03:36:01 ip-10-20-1-182 CROND[2005]: (root) CMD (/opt/aws-scripts-mon/mon-put-instance-data.pl --disk-space-avail --disk-path=/) Jul 24 03:36:01 ip-10-20-1-182 CROND[2007]: (root) CMD (/opt/aws-scripts-mon/mon-put-instance-data.pl --mem-util --mem-used --mem-avail) Jul 24 03:37:01 ip-10-20-1-182 anacron[763]: Job `cron.daily' started Jul 24 03:37:01 ip-10-20-1-182 run-parts(/etc/cron.daily)[2040]: starting logrotate You have new mail in /var/spool/mail/root [root@ip-xxxxxx log]# |
Tail sebagai listener terhadap sebuah file / tracking perubahan file secara realtime
tail -f namafile
Contoh menyandingkan tail dengan pipe grep
1 2 3 4 5 | [root@ip-xxxxxx log]# tail -f awslogs.log |grep "num_of_events': 1" 2022-08-04 09:59:34,169 - cwlogs.push.publisher - INFO - 2015 - Thread-3 - Log group: CMD-LOG, log stream: STG-COMM-DEP-A, queue size: 0, Publish batch: {'skipped_events_count': 0, 'first_event': {'timestamp': 1659581968000, 'start_position': 55694L, 'end_position': 55802L}, 'fallback_events_count': 0, 'last_event': {'timestamp': 1659581968000, 'start_position': 55694L, 'end_position': 55802L}, 'source_id': '1c78100b4e0442a9a293f1e5adc73885', 'num_of_events': 1, 'batch_size_in_bytes': 133} 2022-08-04 10:09:03,528 - cwlogs.push.publisher - INFO - 2015 - Thread-3 - Log group: CMD-LOG, log stream: STG-COMM-DEP-A, queue size: 0, Publish batch: {'skipped_events_count': 0, 'first_event': {'timestamp': 1659582537000, 'start_position': 55802L, 'end_position': 55910L}, 'fallback_events_count': 0, 'last_event': {'timestamp': 1659582537000, 'start_position': 55802L, 'end_position': 55910L}, 'source_id': '1c78100b4e0442a9a293f1e5adc73885', 'num_of_events': 1, 'batch_size_in_bytes': 133} 2022-08-04 10:09:34,607 - cwlogs.push.publisher - INFO - 2015 - Thread-3 - Log group: CMD-LOG, log stream: STG-COMM-DEP-A, queue size: 0, Publish batch: {'skipped_events_count': 0, 'first_event': {'timestamp': 1659582568000, 'start_position': 55910L, 'end_position': 56018L}, 'fallback_events_count': 0, 'last_event': {'timestamp': 1659582568000, 'start_position': 55910L, 'end_position': 56018L}, 'source_id': '1c78100b4e0442a9a293f1e5adc73885', 'num_of_events': 1, 'batch_size_in_bytes': 133} 2022-08-04 10:10:45,735 - cwlogs.push.publisher - INFO - 2015 - Thread-3 - Log group: CMD-LOG, log stream: STG-COMM-DEP-A, queue size: 0, Publish batch: {'skipped_events_count': 0, 'first_event': {'timestamp': 1659582639000, 'start_position': 56447L, 'end_position': 56550L}, 'fallback_events_count': 0, 'last_event': {'timestamp': 1659582639000, 'start_position': 56447L, 'end_position': 56550L}, 'source_id': '1c78100b4e0442a9a293f1e5adc73885', 'num_of_events': 1, 'batch_size_in_bytes': 128} |
Dapatkan bonus wallet untuk setiap transaksi di XM hingga $25 per lot. Buka akun trading Wallstreet, nasdaq, snp500 atau forex
Komentar
Posting Komentar