How to Disable Automatic update and upgrade – Ubuntu

1.Why we have to disable Automatic Update or Upgrade?

When you want to install any application you may get the below messages

root@ubuntu1804:~# sudo dpkg -i mysql-apt-config_0.8.15-1_all.deb

dpkg: error: dpkg frontend is locked by another process

it is because of periodic update running in the background.

Some update may cause to need your machine restart.

2. Disable Automatic Updates from Command Line

Edit /etc/apt/apt.conf.d/20auto-upgrades to disable automatic updates from the command line:

$ sudo nano /etc/apt/apt.conf.d/20auto-upgrades

Once you have the file opened, switch off the Update-Package-Lists directive from 1 to 0 as shown below

APT::Periodic::Update-Package-Lists “0”;

APT::Periodic::Unattended-Upgrade “0”;

How to See the Environment Variables of the Process or Application

Why do we need this?.

You are in the process of investigating an issue and seems that process is not running as expected compared with other environment or compared to previous execution.

There could be chance that your process might have started with wrong environment variables then the regular execution. To identify it you can check the process and its related environment variables when it was started

How to achieve it ?

Method 1 : Add the “e” modifier to “ps” to also show all environment variables

$ ps e -ww -p 96034
PID TTY STAT TIME COMMAND
96034 ? Sl 0:00 /path/to/your/script.sh TERM=linux PATH=/sbin:/usr/sbin:/bin:/usr/bin runlevel=3 RUNLEVEL=3 SUPERVISOR_GROUP_NAME=xxx PWD=/var/www/vhosts/worker.nucleus.be/cron-tasks LANGSH_SOURCED=1 LANG=en_US.UTF-8 previous=N

Method 2: Get the PID and access the environment file under /proc
First, check your PID:
$ ps faux | grep ‘your_process’
508 96034 0.0 0.3 44704 3584 ? Sl 10:10 0:00 _ /path/to/your/script.sh

check the environment variables in /proc/$PID/environ

$ cat /proc/96034/environ

TERM=linuxPATH=/sbin:/usr/sbin:/bin:/usr/binrunlevel=3RUNLEVEL=3SUPERVISOR_GROUP_NAME=xxxPWD=/path/to/your/homedirLANGSH_SOURCED=1LANG=en_US.UTF-8previous=NPREVLEVEL=N

to get that output more readable, you can do two things. Either parse the null character (\0) and replace them by new lines (\n) or use the strings tool that does this for you

$ cat /proc/96034/environ | tr ‘\0’ ‘\n’
TERM=linux
PATH=/sbin:/usr/sbin:/bin:/usr/bin
runlevel=3
RUNLEVEL=3
SUPERVISOR_GROUP_NAME=xxx
PWD=/path/to/your/homedir
LANGSH_SOURCED=1
LANG=en_US.UTF-8
previous=N
PREVLEVEL=N

$ strings /proc/96034/environ
TERM=linux
PATH=/sbin:/usr/sbin:/bin:/usr/bin
runlevel=3
RUNLEVEL=3
SUPERVISOR_GROUP_NAME=xxx
PWD=/path/to/your/homedir
LANGSH_SOURCED=1
LANG=en_US.UTF-8
previous=N
PREVLEVEL=N


How to add bridge network

Problem Statement:
You might have created VM in your laptop which is running on different network segment 10.x.x.x. your laptop is running on wired/wireless network on different segment 192.168.x.x. Now we need to bridge these two network to transfer files and access internet. How to achieve it?
Solution:
In your VM add another network interface as bridged network device name is eth1 and make a note of HWaddr.
Reboot the VM generally allow you to bring eth0 and eth1 both interfaces on by default. now you can access your laptop/pc through the second interface IP segment will be same as your Laptop/PC segement

if not we can manually add the device file and update as below

[root@localhost network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@localhost network-scripts]#

[root@localhost network-scripts]# cat ifcfg-eth0
DEVICE=”eth0″
BOOTPROTO=”dhcp”
HWADDR=”08:00:27:yy:yy:yy”
NM_CONTROLLED=”yes”
ONBOOT=”yes”
[root@localhost network-scripts]# cat ifcfg-eth1
DEVICE=”eth1″
HWADDR=”08:00:27:xx:xx:xx”
ONBOOT=”yes”
NM_CONTROLLED=”yes”
BOOTPROTO=”dhcp”
[root@localhost network-scripts]#
eth0 Link encap:Ethernet HWaddr 08:00:27:yy:yy:yy
inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0

eth1 Link encap:Ethernet HWaddr 08:00:27:xx:xx:xx
inet addr:192.168.100.106 Bcast:192.168.100.255 Mask:255.255.255.0

How to scp from remote server multiple files

What is Secure Copy?

scp allows files to be copied to, from, or between different hosts. It uses ssh for data transfer and provides the same authentication and same level of security as ssh.

Basic syntax of SCP

scp source_file_name username@destination_host:destination_folder

“copy source_file_name” into “destination_folder” at “destination_host” using “username account”.

How to copy single file
$scp -p username@hostname:/u01/local/oracle/db_01/backup/<FileName> .

 

How to copy multiple files
To use wildcard on the remote host, we must delimit the wildcards with backslash (\)
example
$ scp -p username@hostname:/u01/local/oracle/db_01/backup/\*.dmp .

The above command will pass “*.dmp”

How to copy Entire folder

$ scp -p username@hostname:/u01/local/oracle/db_01/backup/\* .

Examples

1.1 Copy the file “foobar.txt” from a remote host to the local host

$ scp your_username@remotehost.edu:foobar.txt /some/local/directory

1.2 Copy the file “foobar.txt” from the local host to a remote host

$ scp foobar.txt your_username@remotehost.edu:/some/remote/directory

1.3 Copy the directory “foo” from the local host to a remote host’s directory “bar”

$ scp -r foo your_username@remotehost.edu:/some/remote/directory/bar

1.4 Copy the file “foobar.txt” from remote host “rh1.edu” to remote host “rh2.edu”

$ scp your_username@rh1.edu:/some/remote/directory/foobar.txt \
your_username@rh2.edu:/some/remote/directory/

1.5 Copying the files “foo.txt” and “bar.txt” from the local host to your home directory on the remote host

$ scp foo.txt bar.txt your_username@remotehost.edu:~

1.6 Copy the file “foobar.txt” from the local host to a remote host using port 2264

$ scp -P 2264 foobar.txt your_username@remotehost.edu:/some/remote/directory
1.7 Copy multiple files from the remote host to your current directory on the local host

$ scp your_username@remotehost.edu:/some/remote/directory/\{a,b,c\} .

How to rotate log files

We can use simple linux commands to rotate logs. There are some tools also available to rotate the logs.
Method 1: Backup the log and make the log empty without stopping the service.
1.1 Backup the current Log

$ tail -100000 alert_UPGR.log > alert_UPGR.log.bak

$ ls -ltr alert_UPGR.log*
-rw-r—– 1 oracle oinstall 353665 Jul 4 00:41 alert_UPGR.log
-rw-r–r– 1 oracle oinstall 353665 Jul 4 00:52 alert_UPGR.log.bak
1.2 Compress the backed up Log

$ gzip alert_UPGR.log.bak
$ ls -ltr alert_UPGR.log*
-rw-r—– 1 oracle oinstall 353665 Jul 4 00:41 alert_UPGR.log
-rw-r–r– 1 oracle oinstall 33727 Jul 4 00:52 alert_UPGR.log.bak.gz
1.3 Empty the active log

$ >alert_UPGR.log

$ ls -ltr alert_UPGR.log*
-rw-r–r– 1 oracle oinstall 33727 Jul 4 00:52 alert_UPGR.log.bak.gz
-rw-r—– 1 oracle oinstall 0 Jul 4 00:53 alert_UPGR.log
1.4 Confirm the alert log is updated

SYS@UPGR:SQL> alter system switch logfile;

System altered.

SYS@UPGR:SQL> exit

$ ls -ltr alert_UPGR.log*
-rw-r–r– 1 oracle oinstall 33727 Jul 4 00:52 alert_UPGR.log.bak.gz
-rw-r—– 1 oracle oinstall 229 Jul 4 00:54 alert_UPGR.log
2. How to rotate Catalina.out log when it is more than 5M?.

2.1. Create this file

/etc/logrotate.d/tomcat

2.2. Copy the following contents into the above file

/var/log/tomcat/catalina.out { copytruncate daily rotate 7 compress missingok size 5M }

About the above configuration:
Make sure that the path /var/log/tomcat/catalina.out above is adjusted to point to your tomcat’s catalina.out
daily – rotates the catalina.out daily
rotate – keeps at most 7 log files
compress – compressesthe rotated files
size – rotates if the size of catalina.out is bigger than 5M

How to run shell script in background

We need sometime run the script in background

1. How to execute the script in Background
Let us say you have created script call test_bg.sh

Login to remote host with appropricate username and password

Syntax
$ nohup <scriptname> > <logname> &
Example:
$ nohup /home/oracle/mudhalvan/scripts/test_bg.sh > /home/oracle/mudhalvan/scripts/test_bg.log &

2. How to check any background jobs are running in this session
$ jobs

3. Test with example
oracle@localhost:~/mudhalvan/scripts$ cat test_bg.sh

find /. -name “test” -print
oracle@localhost:~/mudhalvan/scripts$ ls -ltr test_bg.sh
-rwxr-xr-x 1 oracle oinstall 29 May 27 19:43 test_bg.sh
oracle@localhost:~/mudhalvan/scripts$
oracle@localhost:~/mudhalvan/scripts$ nohup /home/oracle/mudhalvan/scripts/test_bg.sh > /home/oracle/mudhalvan/scripts/test_bg.log &
[1] 3865
oracle@localhost:~/mudhalvan/scripts$ jobs
[1]+ Running nohup /home/oracle/mudhalvan/scripts/test_bg.sh > /home/oracle/mudhalvan/scripts/test_bg.log &
oracle@localhost:~/mudhalvan/scripts$

4. Why we need to run the scripts in background

1. You may be connected remotely to the server and execute some script which need to be executed for more than few hours. Due to network issue or session security you remote session may disconnect which may cause your running script end at half way.

2. Long running script which will not take your one session of your remote client connection from your client machine.

5. Benifits of running a script in background

1. Disconnect or network disturbance on your remote client will not impact your script execution
2. You can use the current remote session for another process