Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
Log rotatation in Linux is so handy as its name implies, it rotates log files. If you have a look at the /var/log path you can see
some compressed files some files which ends with .1

/var/log/my-app.log {
weekly
rotate 4
compress
delaycompress
missingok
notifempty
create 644 root root
}

The location of log rotation configs is in /etc/logrotate.d/. I have created a file in it with the config above.

Explanation of some parameters:
- weekly: weekly says that you want to Linux to rotate your log files weekly, you can also set daily, monthly, yearly.

- rotate 4: it says that how many rotated files should be kept, here I keep 4 rotated files (one month).

- compress: well you tell me what this parameter does.

- delaycompress: some programs do not close file handlers immediately, here we tell log rotate to delay the compression.

- missingok: don't return error if the log file is missing

OK, the list goes on. Take a look at the manual yourself and see its options.

#linux #logrotate #rotate
I have a script that checks a source folder for new files in case there are files in the source folder, it will move those files to destination.

The problem I encountered recently was that files are huge and it may be in the middle of the copying into source by another process so my script tries to move an incomplete file to a destination. Let's say the file is 4GB in size and just only 1GB of the file has been copied. I have to wait until file is 4GB and other handler using that file, then I should safely move the file.

You can use lsof command in order to check which processes are using the source file:


if [[ `lsof -- /var/my-folder/my-big-file.tar.gz` ]]
then
echo "File is being used by a process."
exit 1
fi


NOTE: you can give file directly to lsof using -- or you can use grep command as follow:


lsof | grep /var/my-folder/my-big-file.tar.gz


NOTE2: if you are in a loop use break instead of exit.

NOTE3: if you get command not found, install it using apt-get install lsof

#linux #sysadmin #lsof #grep
How to limit bandwidth of rsync linux command?

rsync is an awesome tool in order to move files via ssh into another server or from server to local system like what scp does but
far better in terms of features and incremental copy mechanism. To limit bandwidth use bwlimit like below:

The general form:

rsync --bwlimit=<kb/second> <source> <dest>

An example of usage with 2MB limit per second transfer:

rsync --bwlimit=2000 /backup/folder user@example-host:/remote/backup/folder/

#linux #sysadmin #rsync #bandwidth
How to remove a user on CentOS 6?

userdel my-target-user

In case you want to remove all the associated user files too use -r parameter:

userdel -r my-target-user

Yes! As simple as that.

#linux #sysadmin #userdel #user #centos
Does that happen for you too, to CD into a wrong directory and need to get back to the previous directory?

Let's say you are in a long path like /mnt/new_volume/backup/files/archive, now you switch to home of your directory. In order to get back to the previous directory, you just need to issue the below command:

cd -

#linux #cd
If you forget to pull your projects from git in a regular interval and many users working on the same projects, then there is a solution for you!

Create a bash script file as follow and make it executable by chmod +x puller.sh:

puller.sh file content:

#!/bin/bash

echo 'Iterating over folders...'
for dir in *
do
test -d "$dir" && {
cd ${dir}
echo "git pull $dir"
git pull
cd ".."
} || {
echo "------> $dir is not a directory <-------"
}
done

NOTE: this file should reside in your folder's project root. In my case it is in /Your/Projects/Folder.

Now as a final step, put it in your crontab:

10 * * * * bash -c "cd /Your/Projects/Folder; bash puller.sh >> /var/log/git_pull_output.log"

#linux #git #pull #cronjob #crontab #cron #bash
We have talked before about how to get current month using the below line of code:

echo $(date +%m)

It prints out 01, 02, etc.

As per the GNU date manpage:

By default, date pads numeric fields with zeroes. The following
optional flags may follow '%':
- (hyphen) do not pad the field

So you can remove leading zero by hyphen as below:

echo $(date +%-m)

It prints out 1, 2, etc.

#linux #bash #date
How to SSH login without password?

You want to use Linux and OpenSSH to automate your tasks. Therefore you need an automatic login from host A / user a to Host B / user b. You don't want to enter any passwords, because you want to call ssh from a within a shell script.

How to do it?
First log in on A as user a and generate a pair of authentication keys. Do not enter a passphrase:

a@A:~> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/a/.ssh/id_rsa):
Created directory '/home/a/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/a/.ssh/id_rsa.
Your public key has been saved in /home/a/.ssh/id_rsa.pub.
The key fingerprint is:
3e:4f:05:79:3a:9f:96:7c:3b:ad:e9:58:37:bc:37:e4 a@A


Now use ssh to create a directory ~/.ssh as user b on B. (The directory may already exist, which is fine):

a@A:~> ssh b@B mkdir -p .ssh
b@B's password:


Finally append a's new public key to b@B:.ssh/authorized_keys and enter b's password one last time:

a@A:~> cat .ssh/id_rsa.pub | ssh b@B 'cat >> .ssh/authorized_keys'
b@B's password:


From now on you can log into B as b from A as a without password:

a@A:~> ssh b@B


#linux #sysadmin #ssh #password_less #ssh_login
How to get default gateway IP address?

ip route | grep default


The output would be something like below:

default via 192.168.1.1 dev eth0 onlink


NOTE: it can be different on your system

#linux #sysadmin #ip #route #default_gateway