Linux Tips and Tricks for beginners

How to run schedule task in Linux using crontab

cron is the time-based job scheduler in Unix-like computer operating systems. cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. It is commonly used to automate system maintenance or administration, though its general-purpose nature means that it can be used for other purposes as well. cron checks for scheduled jobs every minute. Using cron you can't schedule a job that needs to run say every 5 seconds.

Cronjobs are written in the following format:
*    *    *    *    *  command or shell script to be executed
┬    ┬    ┬    ┬    ┬
│    │    │    │    │
│    │    │    │    │
│    │    │    │    └───── day of week (0 - 6) (0 is Sunday, or use names)
│    │    │    └────────── month (1 - 12)
│    │    └─────────────── day of month (1 - 31)
│    └──────────────────── hour (0 - 23)
└───────────────────────── min (0 - 59)

Each user can have their own crontab file. Signon as root to see system wide schedule tasks. To check for existing entries
$ crontab -l 

To edit the crontab file
$ crontab -e 

If you keep all the 5 values to asterisks that means the job will run every minute. For example
* * * * * /usr/local/myscript.sh 

Now if you don't want to specify the full path you can set the PATH in the crontab file
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:
/usr/local
* * * * * myscript.sh

How to send the crontab output to a file for logging
* * * * * /usr/local/myscript.sh &>> /usr/local/crontablog.txt 

Scheduling a job to run everyday at 5AM

0 5 * * * /usr/local/myscript.sh

Scheduling a job to run every Tuesday at 5AM

0 5 * * 2 /usr/local/myscript.sh

Scheduling a job to run only on weekdays at 5AM

0 5 * * 1-5 /usr/local/myscript.sh

Scheduling a job to run everyday at 5AM and 4PM

0 5,16 * * * /usr/local/myscript.sh

Scheduling a job to run only on weekdays between 9AM and 5PM

0 9-17 * * 1-5 /usr/local/myscript.sh

Scheduling a job to run every 5 minutes

*/5 * * * * /usr/local/myscript.sh

Special crontab keywords

@reboot     Run once, at startup
@yearly     Run once  a year     "0 0 1 1 *"
@monthly    Run once  a month    "0 0 1 * *"
@weekly     Run once  a week     "0 0 * * 0"
@daily      Run once  a day      "0 0 * * *"
@hourly     Run once  an hour    "0 * * * *

For example:
@daily /usr/local/myscript.sh

Linux send STDOUT to a file

$ ls -l > report.txt

OR

$ ls -l 1> report.txt

Output in the file report.txt
total 4
-rwxr-xr-x 1 luser luser 78 2012-05-10 19:08 mySample
-rw-rw-r-- 1 luser luser  0 2012-05-13 23:42 report.txt

Linux send STDERR to a file

abcd  2> report.txt

Output in the file report.txt
No command 'abcd' found, did you mean:
 Command 'lbcd' from package 'lbcd' (universe)
 Command 'ascd' from package 'ascd' (universe)
 Command 'abcde' from package 'abcde' (universe)
 Command 'bcd' from package 'bsdgames' (universe)
abcd: command not found

Linux send both STDERR and STDOUT to a file

abcd  &> report.txt

Output in the file report.txt
No command 'abcd' found, did you mean:
 Command 'lbcd' from package 'lbcd' (universe)
 Command 'ascd' from package 'ascd' (universe)
 Command 'abcde' from package 'abcde' (universe)
 Command 'bcd' from package 'bsdgames' (universe)
abcd: command not found

How to find files in Linux with specific permissions


Find all files that have 777 permission
$ find / -type f -perm 0777

Find all files in the current directory with 644 permission
$ find . -type f -perm 0644

How to display file permissions in Linux using Shell command


You need to use ls command with -l option. The first character tell whether its a file or a directory and after that are the permissions. Here is an example

$ ls -l /var/lib/tomcat6

If you don't specify the path it will list files and directories in the current path.

The response to the  above command is something like this
total 20
drwxr-xr-x 3 tomcat6 tomcat6 4096 2012-05-08 20:43 common
lrwxrwxrwx 1 root    root      12 2012-01-26 08:08 conf -> /etc/tomcat6
lrwxrwxrwx 1 root    root      17 2012-01-26 08:08 logs -> ../../log/tomcat6
-rw-r--r-- 1 root    root      20 2012-05-12 16:01 myFile.txt
drwxr-xr-x 3 tomcat6 tomcat6 4096 2012-05-08 20:43 server
drwxr-xr-x 3 tomcat6 tomcat6 4096 2012-05-08 20:43 shared
drwxrwxr-x 3 tomcat6 tomcat6 4096 2012-05-08 20:43 webapps
lrwxrwxrwx 1 root    root      19 2012-01-26 08:08 work -> ../../cache/tomcat6

The very first column shows the file type and permissions. The second column shows the number of links, the third one shows the owner of the file, and the fourth one shows the group the file belongs to. The other columns show the file's size in bytes, date and time of last modification, and the filename.

The first character 
  • d = directory 
  • - = file 
  • l = symbolic link
  • s = socket 
  • p = named pipe 
  • c= character (unbuffered) device file special 
  • b=block (buffered) device file special ;
The file permissions
  • r = Read permission 
  • w = Write permission 
  • x = Execute permission 
  • - = No permission 
The file permissions are defined for the owner level, group level, and the public level (meaning anyone else). The first set of 3 characters are for the user level security basically for the owner of the file. The second set of 3 characters are for those users that belong to the same group as the file and the last 3 characters  are for anyone else who has no affiliation to the file.

How to list only directories?
$ ls -d */ -l

How to find files and then print permissions?
$ find . -type f -exec ls -l {} \;


How to set permissions for all files and directories recursively using chmod

In the current directory
find . -exec chmod 755 {} \; 
-- OR --
chmod -R 755 

Find files starting with a specific string and then change permissions
find /usr/local -name "abc*" -exec chmod 755 {} \;


How to find empty files and directories in linux

find /usr/local -empty
How to delete ?
find /usr/local -empty -delete

How to find only empty files in linux

find /usr/local -type f -empty
How to delete ?
find /usr/local -type f -empty -delete

How to find only empty directories in linux

find /usr/local -type d -empty
How to delete ?
find /usr/local -type d -empty -delete

How to find a file in Linux but skip any mounted file systems

find / -xdev -name "abc.txt"

This will help you skip mounted network drives or filesystems that you do not want searched.

How to find a file in linux anywhere in your file system

find / -name "abc.txt"

This will start the search from the root and drill into every sub-directory to find the file with name abc.txt
If you want to start from a specific folder then change / to that one. For example

find /usr/local -name "httpd.conf"

This will try to find the file in /usr/local and any sub-directories within it.

How to do case insensitive file name search in Linux

find . -iname "vm*"

-name is case sensitive and -iname is case insensitive
the DOT means current directory, you can specify a path instead. For example

find /usr/lib -iname "vm*"

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.