Tag Archives: cron

Linux: My Bash Script Collection / Server Configurations

1. Restart apache daily. Put in /etc/cron.daily directory.

#!/bin/bash
#/usr/bin/systemctl restart httpd
#/usr/bin/systemctl start httpd

for (( ; ; ))
do
echo "checking"
sleep 120
if [ `ps auxwwww|grep httpd|grep -v grep|wc -l` -gt 0 ]
then
echo Apache is running.
break
else
echo Apache is not running.
/usr/bin/systemctl start httpd
fi

done

2. Set Limit File Size Per User Using Limits.conf

#example : limit file size ~100MB per file per user
#create file in /etc/security/limits.d/99-hafez.conf
#enter details below:
--------------- BOF ------------------
# hard fsize
hafez hard fsize 100000
--------------- EOF -----------------
#save the file
#-- tested on centos 6.9

Advertisements

Linux : Scheduling using “cron” and “at”

Basic scheduling commonly use in linux OS.

Cron (best use for repetition task only)

Format: (split each with space)
# min(0-59) hour(0-23) dom(1-31) mon(1-12) dow(0-6 0=sun) command #

Example:
1. Run task every 2 hours
* */2 * * * /path/to/task/file/command

2. Run task every day at 11:30 PM
30 23 * * * /path/to/task/file/command

3. Run task every Sunday at 10.00 pm
00 22 * * 1 /path/to/task/file/command

At (best use for one time task)

from at manual page :

at and batch read commands from standard input or a specified file which are to be executed at a later time, using /bin/sh.

at executes commands at a specified time.

atq lists the user’s pending jobs, unless the user is the superuser; in that case, everybody’s jobs are listed. The format of the output lines
(one for each job) is: Job number, date, hour, queue, and username.

atrm deletes jobs, identified by their job number.

batch executes commands when system load levels permit; in other words, when the load average drops below 0.8, or the value specified in the invoca tion of atd.

Example:

1. Run task on 1:00 PM once
# at 1:00 PM
at > /path/to/task/command
at > ^d

Ctrl + d to exit
Also can use 23:00 for 1:00 PM

2. Run task 2 days from today
# at now + 2 days
at > /path/to/task/command
at > ^d

3. List all task
# atq

4. View detail ( at -c number of task)
# at -c 3

5. Remove task (atrm number of task)
# atrm 3

Reference taken from : http://www.computerhope.com/unix/uat.htm

the expression: would translate to:
noon ---> 12:00 PM October 18 2014
midnight ---> 12:00 AM October 19 2014
teatime ---> 4:00 PM October 18 2014
tomorrow---> 10:00 AM October 19 2014
noon tomorrow ---> 12:00 PM October 19 2014
next week ---> 10:00 AM October 25 2014
next monday ---> 10:00 AM October 24 2014
fri ---> 10:00 AM October 21 2014
NOV---> 10:00 AM November 18 2014
9:00 AM ---> 9:00 AM October 19 2014
2:30 PM ---> 2:30 PM October 18 2014
1430 ---> 2:30 PM October 18 2014
2:30 PM tomorrow ---> 2:30 PM October 19 2014
2:30 PM next month ---> 2:30 PM November 18 2014
2:30 PM Fri ---> 2:30 PM October 21 2014
2:30 PM 10/21 ---> 2:30 PM October 21 2014
2:30 PM Oct 21---> 2:30 PM October 21 2014
2:30 PM 10/21/2014 ---> 2:30 PM October 21 2014
2:30 PM 21.10.14 ---> 2:30 PM October 21 2014
now + 30 minutes ---> 10:30 AM October 18 2014
now + 1 hour ---> 11:00 AM October 18 2014
now + 2 days ---> 10:00 AM October 20 2014
4 PM + 2 days ---> 4:00 PM October 20 2014
now + 3 weeks ---> 10:00 AM November 8 2014
now + 4 months ---> 10:00 AM February 18 2015
now + 5 years ---> 10:00 AM October 18 2019

Remember to check your server time first : # date

Credits to :
https://debian-handbook.info/browse/stable/sect.task-scheduling-cron-atd.html
http://www.computerhope.com/unix/uat.htm
Linux Manual Page

Tested on Centos 6/7