Linux Programmer | RHCE | RHCSA

Search This Blog

Tuesday, 22 November 2022

Schedule a cron job to Run on a First/last day of every month

Execute cron job for first day of every month.

10 10 1 * * /tmp/test.sh

 

The above creates a cron job that executes every 10:10 AM each month on the first day.


Execute cron job on last day of every month.

Determining the last day of the month can be challenging as some months have 28, 29, 30, or 31 as the last date. Besides, the field for the Day_of_Month takes a specific day and giving it a list of possible days would execute on an incorrect day.

 

The trick is to specify a command that checks if the next day is the first day of the month. If so, it means the current day is the last of that month and the cron job can then execute.
 
The command below checks and returns tomorrow’s date: 

date +%d -d tomorrow

we can set it to run our cron job. 

The new command would be:

30 13 28-31 * * [ "$(date +\%d -d tomorrow)" = "01" ]  && /tmp/test.sh

Thursday, 27 January 2022

Execute programs with sudo privilleges with non-root users

Sometimes we don't want normal users to provide sudo rights. but we want to run some scripts which can only run with the sudo.

in that case we can set user rights that will run only specific script with sudo privilleges.

You can perform this by three ways.

Method 1:

 
Create file (vim /etc/sudoers.d/iosys) and add below lines in created file.
 

username ALL=NOPASSWD:/command/to/execute
username ALL=NOPASSWD:/command/to/execute 

Note: Change username accordingly.

Method 2: 


Open below configuration file, and made provided changes into it.

nano /etc/sudoers

Edit it with below lines,


# User privilege specification
 

root    ALL=(ALL:ALL) ALL

ALL ALL =(root) NOPASSWD:/command/to/execute
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL
%sudo   ALL=NOPASSWD:/command/to/execute 




Allow SSH for specific IP without using IPTABLES.

With using below steps you can block and Allow specific IPs to take SSH without using iptables and Firewall.

Open below file,

cat /etc/hosts.allow

sshd: 192.168.0.13 : allow
sshd: ALL : deny

This will allow SSH for 192.168.0.13 IP only.

SSH not working with password after upgrade ubuntu 22.04 or above

In recent upgrade of ubuntu 22.04 and above we are not able to login server with SSH password. but when we try to login with key then it all...