Search

Showing posts with label LINUX. Show all posts
Showing posts with label LINUX. Show all posts

You wish to print a file ‘draft’ with 60 lines to a page. What command would you use?


- The command that I would use is: pr -l60 draft
- The default page length when using pr is 66 lines.
- The -l option specifies a different length.

Which command would you use if you want to remove the password assigned to a group?


- gpasswd – r removes the password from the group.
- Here, the gpasswd changes the password of the group and when it is accompanied by –r, the password gets removed.

Which daemon tracks events on your system?


- The syslogd daemon tracks the system information and saves it to specified log files.

Which account is created on Linux installation?



-With the installation of Linux, a super user account is created called as ‘root’.

How to Modify Crontab file?

*
crontab -e This will open the crontab file and let you edit it. By default this file will be opened with the VI editor and you will need to press the ―Insert‖ key on your keyboard to be able to write in that file. *

 30 13 * * * /home/your_username/run-me.sh >/dev/null 2>&1

The first character you see is ―30‖ this means that crontab will run the script every time the clock hits the 30 minutes mark. Next ―13‖ this means that crontab will run the script when the clock hits 13. The next three * tell crontab to run the script every day, of every month of every weekday. Combining these fields crontab will run the script every day at exactly 13:30. You may notice that we added the ―>/dev/null 2>&1‖ string at the end of the command. The default cron job will always send and e-mail to the root account when ever a command is executed. Now you don‘t want to be notified every day that your crontab job has been executed. If you don‘t want to receive e-mails every day notifying you about your job‘s execution place this ―>/dev/null 2>&1‖ at the end of every instance of every crontab command.

When you are finished adding your commands to the crontab file you need to save and exit. If you are using VI as your editor you need to issue the following commands:

* Press the Esc (Escape key) on your keyboard to enter the command mode of VI *

After you pressed Escape then type the following characters :wq! and press Enter. Remember you have to type this characters (remove the quotes): ―:wq!‖.

Crontab file, Crontab syntax :-

A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.
* * * * * command to be executed
 - – – – -
‗ ‗ ‗ ‗ ‗
‗ ‗ ‗ ‗  +—– day of week (0 – 6) (Sunday=0)
‗ ‗ ‗ +——- month (1 – 12)
 ‗ ‗ +——— day of month (1 – 31)
 ‗ +———– hour (0 – 23)
 +————- min (0 – 59)

* in the value field above means all legal values as in braces for that column. The value column can have a * or a list of elements separated by commas. An element is either a number in the ranges shown above or two numbers in the range separated by a hyphen (meaning an inclusive range).

Note: The specification of days can be made in two fields: month day and weekday. If both are specified in an entry, they are cumulative meaning both of the entries will get executed .

Crontab Commands

export EDITOR=vi ;
to specify a editor to open crontab file. crontab -e Edit your crontab file, or create one if it doesn‘t already exist. crontab -l Display your crontab file. crontab -r Remove your crontab file. crontab -v Display the last time you edited your crontab file. (This option is only available on a few systems.)

Crontab Location, Where is it stored?

It will be stored /var/spool/cron/ but we only root has permission for those directories.

Crontab Restrictions

You can execute crontab if your name appears in the file /usr/lib/cron/cron.allow. If that file does not exist, you can use crontab if your name does not appear in the file /usr/lib/cron/cron.deny. If only cron.deny exists and is empty, all users can use crontab. If neither file exists, only the root user can use crontab. The allow/deny files consist of one user name per line.

What is crontab?

cron is a unix, solaris utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon. These tasks are often termed as cron jobs in unix , solaris. Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times.

Cron Scheduling Task Examples in Linux

1. List Crontab Entries

List or manage the task with crontab command with -l option for current user.

# crontab -l
00 10 * * * /bin/ls >/ls.txt

2. Edit Crontab Entries

To edit crontab entry, use -e option as shown below. In the below example will open schedule jobs in VI editor. Make a necessary changes and quit pressing :wq keys which saves the setting automatically.

# crontab -e

3. List Scheduled Cron Jobs

To list scheduled jobs of a particular user called tecmint using option as -u (User) and -l (List).

# crontab -u tecmint -l
no crontab for tecmint

Note: Only root user have complete privileges to see other users crontab entry. Normal user can’t view it others.

4. Remove Crontab Entry

Caution: Crontab with -r parameter will remove complete scheduled jobs without confirmation from crontab. Use -i option before deleting user’s crontab.

# crontab -r

5. Prompt Before Deleting Crontab

crontab with -i option will prompt you confirmation from user before deleting user’s crontab.

# crontab -i -r
crontab: really delete root's crontab?

6. Allowed special character (*, -, /, ?, #)
  1. Asterik(*) – Match all values in the field or any possible value.
  2. Hyphen(-) – To define range.
  3. Slash (/) – 1st field /10 meaning every ten minute or increment of range.
  4. Comma (,) – To separate items.
7. System Wide Cron Schedule
System administrator can use predefine cron directory as shown below.
  1. /etc/cron.d
  2. /etc/cron.daily
  3. /etc/cron.hourly
  4. /etc/cron.monthly
  5. /etc/cron.weekly
8. Schedule a Jobs for Specific Time
The below jobs delete empty files and directory from /tmp at 12:30 am daily. You need to mention user name to perform crontab command. In below example root user is performing cron job.

# crontab -e

30 0 * * *   root   find /tmp -type f -empty -delete

9. Special Strings for Common Schedule
Strings
Meanings
@reboot
Command will run when the system reboot.
@daily
Once per day or may use @midnight.
@weekly
Once per week.
@yearly
Once per year. we can use @annually keyword also.
Need to replace five fields of cron command with keyword if you want to use the same.

10. Multiple Commands with Double amper-sand(&&)

In below example command1 and command2 run daily.

# crontab -e
@daily <command1> && <command2>

11. Disable Email Notification.

By default cron send mail to user account executing cronjob. If you want to disable it add your cron job similar to below example. Using >/dev/null 2>&1 option at the end of the file will redirect all the output of the cron results under /dev/null.

[root@tecmint ~]# crontab -e
* * * * * >/dev/null 2>&1

conclusion: Automation of tasks may help us to perform our task better ways, error free and efficiently. You may refer manual page of crontab for more information typing ‘man crontab‘ command in your terminal.