Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze nohup Command and cron crontab in linux

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

How to analyze the nohup command and cron crontab in linux? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

1, nohup command description: if you are running a process, and you think the process will not end when you exit your account, then you can use the nohup command. This command can continue to run the corresponding process after you log out of your account. Nohup means not to hang (no hang up)

In shell, the file descriptors are usually: STDIN standard input, STDOUT standard output, STDERR standard error output, namely: 0mem1mem2

Example: nohup abc.sh > nohup.log 2 > & 1 &

Where 2 > & 1 means to redirect STDERR to the file with the same name as the previous standard output, that is, & 1 is nohup.log.

The result is that when a standard error occurs in the executed command, the error will also be output to the output file you specify.

Nohup redirects the output of abc.sh to the file nohup.log and the error is also output to nohup.log, which is then executed in the background.

There are two formats for redirecting standard output and standard error:

& > word

And

> & word

Of the two forms, the first is preferred. This is semantically equivalent to

> word 2 > & 1

2. Cron and crontab

Cron is the main scheduling process of the system, which can run jobs without human intervention. There is a command called crontab that allows users to submit, edit, or delete jobs. Each user can have a crontab file to save scheduling information. You can use it to run any shell script or a command, once an hour, or three times a week, depending on you. Each user can have their own crontab files, but on a larger system, the system administrator generally forbids these files and keeps only one such file on the entire system. The system administrator forbids or allows users to have their own crontab files through cron.deny and cron.allow files.

3.1Domain of crontab

In order to be able to run jobs at a specific time, you need to understand the meaning and format of each field in each entry in the crontab file.

Here are the fields:

Column 1 minutes 1: 5 9

Column 2 hours 1: 2 3 (0 means midnight)

The third day 1: 3 1

The fourth month 1-1-2

The fifth column is 0006 (0 means Sunday)

Column 6 commands to run

Here is the format of c r o n t a b:

Points

< >

Hour

< >

Day

< >

Month

< >

Week

< >

The command to run

Among them

< >

Represents a space.

An entry in the crontab file is read from the left, the first column is minutes, and the last column is the command to run, which is at the end of the week.

In these fields, you can use a horizontal bar to indicate a time range, for example, if you want to run a job from Monday to Friday, you can use 1-5 in the week domain. You can also use a comma "," in these fields, for example, if you want to run a job on Monday and Thursday, you only need to use 1Pol 4 to represent it. You can use an asterisk * to indicate a continuous period of time. If you do not have a special limit on a domain that represents time, you should also enter * in that field. Each entry in the file must contain five time fields, and each field must be separated by a space. All comment lines in the file should be represented by # at the beginning of the line.

3.2.Example of crontab entry

Here are some examples of crontab file entries:

30 21 * / apps/bin/cleanup.sh

The above example shows running cleanup.sh in the / apps/bin directory at 21:30 every night.

45-4-1-10-10-22 * / apps/bin/backup.sh

The above example shows running backup.sh in the / apps/bin directory at 4:45 on the 1st, 10th, and 22nd of each month. You may have noticed that in the above example, each command gives an absolute path. When running a shell script using crontab, it is up to the user to give the absolute path to the script and set the corresponding environment variables. Remember, since the user submitted these jobs to cron, provide cron with all the environments you need. Don't assume that cron knows the special environment you need, but it doesn't. So make sure you provide all the necessary paths and environment variables in the shell script, except for some automatically set global variables. If cron cannot run the script, the user will receive an email explaining why.

3.3.The crontab command options

The general form of the crontab command is:

Crontab [- u user]-e-l-r

Where:

-u user name.

-e Edit the crontab file.

-l lists the contents of the crontab file.

-r Delete the crontab file.

If you log in under your own name, you don't need to use the-u option, because when you execute the crontab command, it knows the current user.

Create a new contab file

Before considering submitting a crontab file to the cron process, one of the first things to do is to set the environment variable EDITOR. The cron process uses it to determine which editor to use to edit the crontab file. 99% of UNIX and LINUX users use vi, and if you do the same, edit the .profile file in the $HOME directory and add a line like this:

EDITOR=vi; export EDITOR

Then save and exit.

You might as well create a file called cron, where is the user name. To submit the crontab file you just created, you can use the newly created file as an argument to the cron command:

$crontab davecron

Now that the file has been submitted to the cron process, a copy of the newly created file has been placed in the / var/spool/cron directory, and the file name is the user name (that is, dave).

3.5.List crontab files

To list crontab files, you can use:

$crontab-l

3.6. edit the crontab file

If you want to add, delete, or edit entries in the crontab file, and the EDITOR environment variable is set to vi, you can edit the crontab file with vi, with the following command:

$crontab-e

You can modify the crontab file and exit as you would any other file using vi.

Delete the crontab file

To delete the crontab file, you can use:

$crontab-r

3.8.Restoration of lost crontab files

If you accidentally delete the crontab file, assuming you have a backup in your $HOME directory, you can copy it to / var/spool/cron/, where is the user name. "if the copy cannot be completed due to permission issues, you can use:"

$crontab

Where is the file name of your copy in the $HOME directory.

After reading the above, have you mastered how to analyze the nohup command and cron crontab in linux? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report