Showing posts with label Bash. Show all posts
Showing posts with label Bash. Show all posts

Sunday, 28 January 2018

Linux command for Base64 encode and decode

Linux has base64 command to encode and decode using Base64 representation. Here is an example : To encode a string Chetna Chaudhari you can use following command:

echo "Chetna Chaudhari" | base64
Q2hldG5hIENoYXVkaGFyaQo=

You can enable debug mode using -d flag to see more details :

echo "Chetna Chaudhari" | base64 -d
May 16 10:56:35 Chetna.local base64[26454] <Info>: Read 17 bytes.
May 16 10:56:35 Chetna.local base64[26454] <Info>: Wrote 24 bytes.
Q2hldG5hIENoYXVkaGFyaQo=

To decode the encoded text,

echo Q2hldG5hIENoYXVkaGFyaQo= | base64 --decode
Chetna Chaudhari

You can check more details using following command:

echo Q2hldG5hIENoYXVkaGFyaQo= | base64 -d --decode
May 16 10:56:37 Chetna.local base64[26431] <Info>: Read 25 bytes.
May 16 10:56:37 Chetna.local base64[26431] <Info>: Decoded to 17 bytes.
Chetna Chaudhari
May 16 10:56:37 Chetna.local base64[26431] <Info>: Wrote 17 bytes.

Saturday, 27 January 2018

How to enable DebugFS on Linux System

Debugfs is Debug Filesystem , its RAM based filesystem which can be used for kernel debugging information. This makes kernel space information available in user space.

How to enable debugfs :

To enable it for onetime, i.e information will be available until next boot of system.
mount -t debugfs none /sys/kernel/debug
To make the change permanent, add following line to /etc/fstab file.
debugfs    /sys/kernel/debug      debugfs  defaults  0 0
Once you enable debugfs, you can see multiple directories inside /sys/kernel/debug :
[root@sandbox ~] ls /sys/kernel/debug 
bdi    boot_params  dynamic_debug  gpio  kprobes  sched_features  usb  xen
block  dma_buf      extfrag        hid   mce      tracing         x86
These files holds information about kernel subsystems which helps in debugging.

Tuesday, 26 January 2016

How to setup cron job for last day of month

You can run any command or script any time or repeatedly with the help of linux utility ‘cron’. To add a cron, just run command 'crontab -e’, this will open a file with crontab entries if any.

The format of a cron line is like below:

MIN HOUR DOM MON DOW CMD


Here,
MIN - Minute field, which can have values between 0 - 59
HOUR - Hour field, which can have values between 0 - 23
DOM - Day of the Month field, values 1 - 31
MON - Month field, values 1 - 12
DOW - Day of Week field, values 0 - 6
CMD - Command field, here you can mention the command which you want to schedule.

For example, if you want to run ‘/home/chetna/gather_stats.sh’ at 27th January at 2.30 pm. Then you can add a cron entry like below:

30 14 27 01 * /home/chetna/gather_stats.sh

So, how do you write a cron to run on last day of month? The problem is, we don’t have a number to put in DOM field, as it could be 28, 30, 31 days in month, sometimes 29.
For eg.

59 23 28-31 * * /home/chetna/gather_stats.sh

This will run the script on 28,29,30 and 31st of every month at 11.59 pm. But in our case, if a month has 31 days, say January, we don’t want to run our script on other 3 days.It should only run on 31 January.
But in all the months, the next day will be 1. So we can use 'date’
to check if next day is 1, and if yes, run my script.
This is how I achieved the task:

59 23 28-32 * * [‘$(date +%d -d tomorrow)’ == ’01’ ] && /home/chetna/gather_stats.sh

Here,

date +%d -d tomorrow
will give tomorrows date as two character string. So we can check it, if it matches “01”


Friday, 17 July 2015

Error during apt-get update - Can't exec "insserv"

Today, While doing an apt-get update on a box, I faced the following issue:
Setting up initscripts (2.88dsf-34) ...
Can't exec "insserv": No such file or directory at /usr/sbin/update-rc.d line 406.
update-rc.d: error: insserv rejected the script header
dpkg: error processing initscripts (--configure):
subprocess installed post-installation script returned error exit status 255
This was the first time I was seeing this issue, so was curious to know,  what is insserv?.  Insserv command is used to control the start and stop order of the services that are on a Linux system. How I fixed the problem? After checking the details about this, I found that insserv symlink was broken. 
sudo ln -s /usr/lib/insserv/insserv /sbin/insserv 
above command fixed the issue.

Sunday, 24 May 2015

Do mkdir and cd using a single command?

Most of the times, when you create a new directory, you may cd to it, to do some work.
chetna.chaudhari@Chetna:~$ mkdir -p /a/b/c
chetna.chaudhari@Chetna:~$ cd /a/b/c
chetna.chaudhari@Chetna:~$ pwd
/a/b/c

How about combining these two commands into a single one.? yes its doable. You
to add following snippet to your .bash_profile file and relaunch a shell.
function mkdircd () { mkdir -p "$@" && eval cd "\"\$$#\""; }
Now do mkdir and cd using a single command
chetna.chaudhari@Chetna:~$ mkdircd /x/y/z
chetna.chaudhari@Chetna:~$ pwd
/x/y/z

Thursday, 9 April 2015

How to enable date timestamp in bash history.

Many times while debugging I have a question, when did I execute this command? Here is a way to enable date and timestamp while listing your bash history.

Without date and timestamp, your history will look like
chetna.chaudhari@Chetna:~$ history
ps aux
jps
ls
clear
history

To enable date and timestamp,

chetna.chaudhari@Chetna:~$ export HISTTIMEFORMAT='%F %T '
Here, %F enables date in yyyy-mm-dd format (%Y-%m-%d) %T enables time in hour:minutes:seconds (%H:%M:%S) So now your history should look like
chetna.chaudhari@Chetna:~$ history
1  2015-04-08 19:49:35 ps aux
2  2015-04-08 19:49:35 jps
3  2015-04-08 19:49:35 ls
4  2015-04-08 19:49:35 clear
5  2015-04-08 19:49:36 ps aux | grep sshd
6  2015-04-08 19:49:37 history

To make it permanent add the export command to your .bash_profile file.