Launching and processing background processes: job management. Processes and job management Running a command in the Linux background

💖 Do you like it? Share the link with your friends

Launch and Processing background processes: job management

You may have noticed that after you entered the command into Terminal "e, you usually need to wait for it to complete before shell will give you back control. This means that you ran the command inpriority mode . However, there are times when this is not desirable.

Let's say, for example, that you decide to recursively copy one large directory to another. You also chose to ignore errors, so you redirected the error channel to/dev/null :

cp -R images/ /shared/ 2>/dev/null

This command may take several minutes to complete. You have two solutions: the first is cruel, implying stopping (killing) the command, and then executing it again, but at a more appropriate time. To do this, click Ctrl+c : This will end the process and take you back to the prompt. But wait, don't do that just yet! Read on.

Let's say you want a command to execute while you do something else. The solution would be to run the process inbackground . To do this, click Ctrl+z to pause the process:

In this case, the process will continue its work, but as a background task, as indicated by the sign & (ampersand) at the end of the line. You will then be taken back to the prompt and can continue working. A process that runs as a background task, or in background, called backgroundtask .

Of course, you can immediately run processes as background tasks by adding the sign & at the end of the command. For example, you can run the directory copy command in the background by typing:

cp -R images/ /shared/ 2>/dev/null &

If you want, you can also restore this process to foreground and wait for it to complete by typingfg (ForeGround - priority). To put it back into the background, enter the following sequence Ctrl+z , bg .

In this way, you can run several tasks: each command will be assigned a task number. Team shell "a jobs displays a list of all jobs associated with the current one shell "om. Before the task there is a sign + , marking the last process running in the background. To restore a specific job to priority mode, you can enter the commandfg , Where - task number, for example,fg 5 .

The Linux terminal service runs in single-tasking mode by default. This means that any crew launched blocks the terminal until it is completed. This approach is not convenient when running programs that require a long execution time. This problem can be solved in two ways: open additional window terminal and run another command in it or use the background. All current operating systems, including Linux, are multitasking, which means the ability to simultaneously execute multiple programs.

How to run a brigade in the background to immediately access the interface command line? A crew that has been forced to run is called a background process. Background processes are not shown on the screen. For example, the Apache HTTPD server runs in the background to serve web pages. You can put a shell script or any command into low priority mode. A task (for example, a crew or a script) can be put into the background by adding an “&” to the end of the command line. This statement puts the command into the background and frees up space in the terminal. A team that runs in the background is called a job. While the background command is running, it is possible to execute any other commands. The syntax is as follows:

command & script-name & /path/to/command arg1 arg2 & command-1 | command-2 arg1 & command-1 | command-2 -arg1 -arg2 >/path/to/output &

To run programs in the background without blocking the terminal window, you must use the special “&” instructor. Place this character at the very end of the line after specifying the command name, options, and input parameters. In general, this sequence can be written as “command_name -option input_parameter &”.

ls ~/* > ~/test-file.txt &
18960

After pressing the Enter button, the program will automatically start in the background. In this case, the terminal will display a line with the following content “[task_number] process_identifier” and will prompt you to enter a newly created command.

Find commands running in the background in Linux

Run the following command:

Example of data output:

Running find / -iname "*.c" 2> /dev/null > /tmp/output.txt &
+ Running grep -R "hostNamed" / 2> /dev/null > /tmp/grep.txt &

Where are the order identifiers.

To display process IDs for job IDs in addition to the standard casts, pass the -l option:

Example of data output:

7307 Running find / -iname "*.c" 2> /dev/null > /tmp/output.txt &
+ 7324 Running grep -R "hostNamed" / 2> /dev/null > /tmp/grep.txt &

To display only process IDs, enter:

Example of data output:

Stop executing commands running in the background

To forcefully or gracefully terminate a process, use the kill command. The syntax is as follows:

kill PID
kill -15 PID
kill -9 PID
killall process-Name-Here
killall -15 process-Name-Here
killall -9 process-Name-Here

Returning a program to foreground mode in Linux

Linux allows you not only to run programs in the background, but also to return them to normal execution if you wish. There are two tools for this: the command ( A team is a group of people united by common motives and interests.) fg and the % operator. The principle of their effort is extremely simple. fg requires specifying the job number as a parameter, and it must be substituted for % immediately after the operator without spaces.

find / -name .ini 2> ~/results.txt &
19090
fg 1
bash: fg: task ended
+ Exit 1 find / -name .ini 2> ~/results.txt

Essentially, the operating system consists of a kernel and a huge set of programs that are designed to perform various tasks, maintain the system and meet the needs of the user. Almost all user interaction and operating system performed using programs. Therefore, it is important for beginners to understand how to run a program on Linux, what happens during startup and what are the launch methods.

Before we move on to launching programs, we must first understand what a program is. In Linux, programs differ from other files only in that they have an executable flag set. I have already written about this in detail in the article, so I will not repeat it.

All programs can be divided into several types:

  • Binary programs- contain instructions to the processor that are ready for execution, most programs are in this format, they are fast and are executed immediately by the system;
  • Bytecode programs- these are no longer processor instructions, but instructions of a specific virtual machine that can execute them; without a virtual machine, such commands cannot be executed. Such programs consume more resources, but are also quite fast, their advantage is that they can be executed without modification anywhere they can work virtual machine. Such programs include Java programs.
  • Script programs- these programs consist of a set of commands in plain text that are executed by a special interpreter. Such programs are slower, but they are easier to develop and their code can be easily and quickly changed.

Now let's move on to launching programs.

Running programs in the terminal

Initially, Unix and Linux operating systems did not have a graphical interface, so programs were launched using commands from the terminal. Now this is also possible and is quite actively used by experienced users. The program launch syntax looks like this:

/path/to/file/program options

Parameters are specified only when they are needed, but the shell must always know the full path to the program. Everything after the program name and a space are parameters. You've probably already noticed that we usually don't specify the full path when executing programs. It would be very long and inconvenient.

The developers have come up with a workaround. A PATH variable was created, which stores all the paths to the folders where programs are usually located - /bin, /sbin, /usr/bin, /usr/sbin and so on. You can view its contents with the command:

When you type the name of a program, the system searches for an executable file with that name in all folders from the PATH and if it finds it, it executes it. If there is no such file, then a message is displayed - command not found. So, to run one of system programs just type the name of its executable file, for example:

And you can pass parameters after a space:

When the program is not in these directories, you need to specify the full path to it:

/usr/local/bin/ls1

If you want to run the program via ubuntu terminal, which is located in the current folder, then the situation will be slightly different. The system only searches the folders in the PATH variable; it does not search the current directory. Therefore, if you type the name of the executable file, you will get an error. You need to specify the full path, as you remember it will be./:

Sometimes it becomes necessary to transfer some special . For example, the EDITOR variable indicates which text editor should be used as default. You can specify a variable name and its value before the command name using the syntax:

variable_name = value command

For example:

EDITOR=nano visudo

By default, this command opens the settings in the Vim editor, but with this environment variable the settings will open in the nano editor.

Running programs as another user

You already know how to run the program in linux terminal, what about other users? In Windows, it is quite common to run programs as an administrator so that the program can gain more access rights in the system. On Linux, the sudo utility is used for this. Her name can be deciphered as s witch u ser do- change user and execute. By default, the utility runs the command as the root superuser:

sudo command
sudo whoami

But using the -u option, you can run the program as any user logged in to the system:

sudo -u username command
sudo -u postgres whoami

The whoami (who am I) command displays the name of the current user.

How to run a program in the background

Sometimes it becomes necessary to run a long-running program in the terminal so that it does not interfere with further work. To do this, you can use running the program in the background on Linux:

program_name &

For example:

dd if=/dev/zero of=~/file count=100000 &

The system will output the PID, the program's unique identifier, which you can then use to close it:

How to run a script on Linux

We have already said that programs are divided into binary and interpreted. Previously, we only talked about binary programs. To run interpreted programs, you need an interpreter directly; such programs include those written in languages ​​such as Java, Python, Perl, Ruby, PHP, NodeJS and many others. The syntax for launching such a program is different:

interpreter /path/to/file/program options

Different interpreters behave differently, so it is better to immediately specify the full path to the program. Python usually picks up scripts from the current folder without specifying the full path:

python hellowrld.py

And Java programs need to be launched like this:

java -jar program.jar

For interpreted program files, the executability flag is optional, since they are passed as a parameter to the main program. Only Bash scripts are an exception. You can run the script with an interpreter:

Or just type the path to the script:

The shell itself determines its scripts by the executability flag and executes them. If the executability flag is not set, then you should add it:

sudo chmod u+x ./script.sh

Therefore, for most interpreted programs, simple sh scripts have been created that can quickly be launched.

Running Linux programs in a GUI

It is much more convenient to launch programs via GUI. If it is impossible to launch console programs this way, then there are shortcuts for all graphical utilities that you can find in the main menu of the system:

In addition, you can run the program from the file manager by double-clicking the mouse, but then the executability flag must be set for it.

Running scripts in a GUI works the same way. You can find all the menu shortcuts in the /usr/share/applications/ directory. Any program can be launched by double clicking from here. But let's see what's inside the shortcut; to do this, open it in a text editor:


Among other things, the Exec line specifies the command that runs linux programs when you double click on a shortcut. You can take one of the existing shortcuts and make your own based on it. This is simply the name of the program. But it is important to note that it is better to specify the full path in places such as shortcuts, scripts, cron and so on, this will reduce the number of errors, since you cannot know whether the system in this case is checking PATH or looking for the program only in the current directory. Now you know everything about how to run a program on Linux.

conclusions

In this article we looked at how to run a program through the ubuntu terminal or in other Linux distributions. Even though this seems like a very simple topic, there are some interesting points, which may be useful. But you already know about them. If you have any questions, ask in the comments!

Like any other multitasking system, Linux runs several processes simultaneously. In general, I will not mislead you, because a regular single-processor computer can only perform one task per this moment time, which is why Linux queues processes for execution.

There are several basic commands for managing processes

  • ps- display a list of running processes
  • kill- sends a signal to one or more processes (basically to "kill" them)
  • jobs- an alternative way to view processes running by you
  • bg- puts the process execution in the background
  • fg- brings the process execution out of the background

Although it may seem that this knowledge is quite abstract, it can find its way practical use even for the average user who uses a GUI. You may not yet know that the majority graphics programs(if not all) can be launched using the command line. For example, let's try to launch the browser, I think most Linux devices have or Google Chrome or FireFox

Enej@linux:/home/pub/www/vv$ google-chrome Created new window in existing browser session.

You can specify the URL you want to open

Enej@linux:/home/pub/www/vv$ google-chrome http://site Created new window in existing browser session.

I already have Chrome running via the GUI, so the browser reports that a new window is being created in an existing session and gives control to the command line. Or, for example, if you need to open a graphic file manager with root rights (the example will work for the Gnome graphical shell with Nautilus installed)

Enej@linux:/home/pub/www/vv$ sudo nautilus /root/ password for enej:

Since the process was not started in the background, you can no longer perform any actions in this terminal window (you can press CTRL + C to end the process).

Background mode

To make life easier, when running graphical programs or processes that can run for a long time, their execution is usually put in the background, for example, let's run the browser in the background

Enej@linux:/home/pub/www/vv$ google-chrome http://site & 9248

Ampersand sign ( & ) at the end of the command call means that the process execution should be placed in the background. In this case, it remains possible to work with this terminal window.

But suddenly you forgot to put an ampresant at the end of the call, then there is another way. You must first stop execution by pressing CTRL + Z, then we get control of the command line and can call the command bg, which we will put last running process into the background. Told by example

Enej@linux:/home/pub/www/vv$ google-chrome http://site ^Z + Stopped google-chrome http://site enej@linux:/home/pub/www/vv$ bg + google- chrome http://site &

Team fg Brings the last running process out of the background.

List of running processes

Since you now know how to run commands in the background, it was good to see a list of them. To do this, use the command jobs or more powerful ps

Enej@linux:/home/pub/www/vv$ jobs - Running nm-applet & (wd: ~/bin) + Running google-chrome http://site & enej@linux:/home/pub/www/vv $ ps PID TTY TIME CMD 2304 pts/0 00:00:02 bash 11104 pts/0 00:00:01 chrome 11108 pts/0 00:00:00 chrome 11110 pts/0 00:00:00 chrome 11132 pts/0 00:00:00 chrome 12088 pts/0 00:00:00 ps 21165 pts/0 00:00:27 nm-applet

How to "kill" a process?

If a process stops responding (i.e. hangs), it must be forcibly “killed”. I think those who use FireFox have encountered this. For such tasks the command is used kill. But first you need to somehow define the process. To do this you can use the command jobs or ps. Using the first, you can find out the process number, and using the second, its identifier.

Enej@linux:/home/pub/www/vv$ google-chrome http://freaksidea..com & 15181 enej@linux:/home/pub/www/vv$ ps PID TTY TIME CMD 2304 pts/0 00: 00:02 bash 15181 pts/0 00:00:00 chrome 15238 pts/0 00:00:00 ps 21165 pts/0 00:00:27 nm-applet enej@linux:/home/pub/www/vv$ kill 15181

Teams fg And bg as the first argument they can take the number of the running process for its subsequent input/output to/from the background.

Learn more about kill

Actually the team kill used to send various signals to processes. It's just that in most cases this signal tells the command that it should terminate. If programs are written correctly, they listen for various signals from the operating system and respond to them. For example, a text editor must listen for any signal that notifies that users are being logged off or that the computer is shutting down. When he (the text editor) "heard" such a signal, he should save open documents before finishing your work. Team kill can send several types of signals, to find out which ones, enter kill -l. Below is a list of the most frequently used signals (the number in brackets is the signal number)

  • SIGNUP(1)- was originally intended to inform the program about the loss of communication with the control terminal (terminals were often connected to the system using modems, so the name of the signal comes from hung up - hang up). The SIGHUP signal is also sent to the application if the session leader process has completed its work. Many daemon programs that do not have a session leader also process this signal. In response to receiving a SIGHUP, the daemon will typically restart (or simply re-read the configuration file). By default, the program that receives this signal terminates.
  • SIGINT(2)- usually sent to a process if the terminal user has given a command to terminate the process (usually this command is the keyboard shortcut Ctrl + C)
  • SIGTERM(15)- causes a "polite" termination of the program. Having received this signal, the program can perform the necessary operations before completion (for example, saving open documents). Receiving SIGTERM does not indicate an error in the program, but rather the desire of the OS or the user to terminate it.
  • SIGKILL(9)- causes force termination program operation. The program can neither process nor ignore this signal.

Default command kill sends SIGTERM signal, but you can also specify the signal number or its name. Let's say that your Chrome is frozen (sometimes my flash player stops working adequately)

Enej@linux:/home/pub/www/vv$ google-chrome http://site & 22066 enej@linux:/home/pub/www/vv$ jobs - Running nm-applet & (wd: ~/bin) + Running google-chrome http://site & enej@linux:/home/pub/www/vv$ kill -SIGTERM %2 enej@linux:/home/pub/www/vv$ kill -SIGKILL %2 enej@linux :/home/pub/www/vv$ kill -9 %2

P.S.: I think 2 commands will be very interesting for beginners: notify-send(sends a message through the graphical shell, appears in the top right in Gnome) and espeak(speech synthesizer). If you don’t have any of them, you can install them using the command apt-get

Enej@linux:/home/pub/www/vv$ sudo apt-get install espeak notify-send

Last time we talked about working with input, output, and error streams in bash scripts, file descriptors, and stream redirection. Now you already know enough to write something of your own. At this stage of mastering bash, you may well have questions about how to manage running scripts and how to automate their launch.

So far, we've typed script names into the command line and pressed Enter, which caused the programs to run immediately, but that's not the only way to call scripts. Today we will talk about how a script can work with Linux signals, about different approaches to running scripts and managing them while running.

Linux Signals

In Linux, there are more than three dozen signals that are generated by the system or applications. Here is a list of the most commonly used ones that will surely come in handy when developing command line scripts.
Signal code
Name
Description
1
SIGHUP
Closing the terminal
2
SIGINT
Signal to stop the process by the user from the terminal (CTRL + C)
3
SIGQUIT
Signal to stop a process by the user from the terminal (CTRL + \) with a memory dump
9
SIGKILL
Unconditional termination of the process
15
SIGTERM
Process termination request signal
17
SIGSTOP
Forcing a process to be suspended but not terminated
18
SIGTSTP
Pausing a process from the terminal (CTRL+Z) but not shutting down
19
SIGCONT
Continue execution of a previously stopped process

If the bash shell receives a SIGHUP signal when you close the terminal, it exits. Before exiting, it sends a SIGHUP signal to all processes running in it, including running scripts.

The SIGINT signal causes operation to temporarily stop. The Linux kernel stops allocating processor time to the shell. When this happens, the shell notifies processes by sending them a SIGINT signal.

Bash scripts do not control these signals, but they can recognize them and execute certain commands to prepare the script for the consequences caused by the signals.

Sending signals to scripts

The bash shell allows you to send signals to scripts using keyboard shortcuts. This comes in very handy if you need to temporarily stop a running script or terminate its operation.

Terminating a process

The CTRL + C key combination generates a SIGINT signal and sends it to all processes running in the shell, causing them to terminate.

Let's run the following command in the shell:

$sleep 100
After that, we will complete its work with the key combination CTRL + C.


Terminate a process from the keyboard

Temporarily stopping the process

The CTRL + Z key combination generates the SIGTSTP signal, which suspends the process but does not terminate it. Such a process remains in memory and its work can be resumed. Let's run the command in the shell:

$sleep 100
And temporarily stop it with the key combination CTRL + Z.


Pause the process

Number in square brackets is the job number that the shell assigns to the process. The shell treats processes running within it as jobs with unique numbers. The first process is assigned number 1, the second - 2, and so on.

If you pause a job bound to a shell and try to exit it, bash will issue a warning.

You can view suspended jobs with the following command:

Ps –l


Task list

In the S column, which displays the process status, T is displayed for suspended processes. This indicates that the command is either suspended or in a trace state.

If you need to terminate a suspended process, you can use kill command. You can read details about it.

Her call looks like this:

Kill processID

Signal interception

To enable Linux signal tracking in a script, use the trap command. If the script receives the signal specified when calling this command, it processes it independently, while the shell will not process such a signal.

The trap command allows the script to respond to signals that would otherwise be processed by the shell without its intervention.

Let's look at an example that shows how the trap command specifies the code to be executed and a list of signals, separated by spaces, that we want to intercept. In this case it is just one signal:

#!/bin/bash trap "echo " Trapped Ctrl-C"" SIGINT echo This is a test script count=1 while [ $count -le 10 ] do echo "Loop #$count" sleep 1 count=$(($ count + 1)) done
The trap command used in this example prints a text message whenever it encounters a SIGINT signal, which can be generated by pressing Ctrl + C on the keyboard.


Signal interception

Every time you press CTRL + C , the script executes the echo command specified when calling trace instead of letting the shell terminate it.

You can intercept the script exit signal by using the name of the EXIT signal when calling the trap command:

#!/bin/bash trap "echo Goodbye..." EXIT count=1 while [ $count -le 5 ] do echo "Loop #$count" sleep 1 count=$(($count + 1)) done


Intercepting the script exit signal

When the script exits, either normally or due to a SIGINT signal, the shell will intercept and execute the echo command.

Modification of intercepted signals and cancellation of interception

To modify signals intercepted by the script, you can run the trap command with new parameters:

#!/bin/bash trap "echo "Ctrl-C is trapped."" SIGINT count=1 while [ $count -le 5 ] do echo "Loop #$count" sleep 1 count=$(($count + 1) ) done trap "echo "I modified the trap!"" SIGINT count=1 while [ $count -le 5 ] do echo "Second Loop #$count" sleep 1 count=$(($count + 1)) done


Signal interception modification

After modification, the signals will be processed in a new way.

Signal interception can also be canceled by simply executing the trap command, passing it a double dash and the signal name:

#!/bin/bash trap "echo "Ctrl-C is trapped."" SIGINT count=1 while [ $count -le 5 ] do echo "Loop #$count" sleep 1 count=$(($count + 1) ) done trap -- SIGINT echo "I just removed the trap" count=1 while [ $count -le 5 ] do echo "Second Loop #$count" sleep 1 count=$(($count + 1)) done
If the script receives a signal before the interception is canceled, it will process it as specified in current team trap. Let's run the script:

$ ./myscript
And press CTRL + C on the keyboard.


Signal intercepted before interception was cancelled.

The first press of CTRL + C occurred at the time of script execution, when signal interception was in effect, so the script executed the echo command assigned to the signal. After execution reached the unhook command, the CTRL + C command worked as usual, terminating the script.

Running command line scripts in the background

Sometimes bash scripts take a long time to complete a task. However, you may need to be able to work normally on the command line without waiting for the script to complete. It's not that difficult to implement.

If you've seen the list of processes output by the ps command, you may have noticed processes that are running in the background and not tied to a terminal.
Let's write the following script:

#!/bin/bash count=1 while [ $count -le 10 ] do sleep 1 count=$(($count + 1)) done
Let's run it by specifying the ampersand character (&) after the name:

$ ./myscipt &
This will cause it to run as a background process.


Running a script in the background

The script will be launched in a background process, its identifier will be displayed in the terminal, and when its execution is completed, you will see a message about this.

Note that although the script runs in the background, it continues to use the terminal to output messages to STDOUT and STDERR, meaning that the text it outputs or error messages will be visible in the terminal.


List of processes

With this approach, if you exit the terminal, the script running in the background will also exit.

What if you want the script to continue running after closing the terminal?

Executing scripts that do not exit when closing the terminal

Scripts can be executed in background processes even after exiting the terminal session. To do this, you can use the nohup command. This command allows you to run a program by blocking SIGHUP signals sent to the process. As a result, the process will be executed even when you exit the terminal in which it was launched.

Let's apply this technique when running our script:

Nohup ./myscript &
This is what will be output to the terminal.


Team nohup

The nohup command untethers a process from the terminal. This means that the process will lose references to STDOUT and STDERR. In order not to lose the data output by the script, nohup automatically redirects messages arriving in STDOUT and STDERR to the nohup.out file.

Note that if you run multiple scripts from the same directory, their output will end up in a single nohup.out file.

View assignments

The jobs command allows you to view current jobs that are running in the shell. Let's write the following script:

#!/bin/bash count=1 while [ $count -le 10 ] do echo "Loop #$count" sleep 10 count=$(($count + 1)) done
Let's run it:

$ ./myscript
And temporarily stop it with the key combination CTRL + Z.


Running and pausing a script

Let's run the same script in the background, while redirecting the script's output to a file so that it does not display anything on the screen:

$ ./myscript > outfile &
By now executing the jobs command, we will see information about both the suspended script and the one running in the background.


Getting information about scripts

The -l switch when calling the jobs command indicates that we need information about process IDs.

Restarting suspended jobs

To restart the script in the background, you can use the bg command.

Let's run the script:

$ ./myscript
Press CTRL + Z, which will temporarily stop its execution. Let's run the following command:

$bg


bg command

The script is now running in the background.

If you have multiple suspended jobs, you can pass the job number to the bg command to restart a specific job.

To restart a job in normal mode use the fg command:

Planning to run scripts

Linux provides a couple of ways to run bash scripts in specified time. These are the at command and the cron job scheduler.

The at command looks like this:

At [-f filename] time
This command recognizes many time formats.

  • Standard, indicating hours and minutes, for example - 10:15.
  • Using AM/PM indicators, before or after noon, for example - 10:15PM.
  • Using special names such as now , noon , midnight .
In addition to being able to specify the time at which a job should run, the at command can also be passed a date using one of its supported formats.
  • A standard date format in which the date is written using the patterns MMDDYY, MM/DD/YY, or DD.MM.YY.
  • A text representation of the date, for example, Jul 4 or Dec 25, while the year can be specified, or you can do without it.
  • Recording like now + 25 minutes .
  • Recording view 10:15PM tomorrow .
  • Recording type 10:15 + 7 days.
Let's not go deeper into this topic, let's look at a simple use case for the command:

$ at -f ./myscript now


Scheduling jobs using the at command

The -M switch when calling at is used to send what the script outputs to e-mail, if the system is configured accordingly. If sending email is not possible, this switch will simply suppress the output.

To view the list of jobs waiting to be executed, you can use the atq command:


List of pending tasks

Deleting pending jobs

The atrm command allows you to delete a pending job. When calling it, indicate the task number:

$atrm 18


Deleting a job

Run scripts on a schedule

Scheduling your scripts to run once using the at command can make life easier in many situations. But what if you need the script to be executed at the same time every day, or once a week, or once a month?

Linux has a crontab utility that allows you to schedule scripts that need to be run regularly.

Crontab runs in the background and, based on data in so-called cron tables, runs scheduled jobs.

To view an existing cron job table, use the following command:

$ crontab –l
When scheduling a script to run on a schedule, crontab accepts data about when the job needs to be executed in the following format:

Minute, hour, day of the month, month, day of the week.
For example, if you want a certain script named command to be executed every day at 10:30, this will correspond to the following entry in the task table:

30 10 * * * command
Here, the wildcard "*" used for the day of month, month, and day of week fields indicates that cron should run the command every day of every month at 10:30 AM.

If, for example, you want the script to run at 4:30PM every Monday, you will need to create the following entry in the tasks table:

30 16 * * 1 command
The numbering of the days of the week starts from 0, 0 means Sunday, 6 means Saturday. Here's another example. Here the command will be executed at 12 noon on the first day of every month.

00 12 1 * * command
Months are numbered starting from 1.
In order to add an entry to a table, you need to call crontab with the -e switch:

Crontab –e
Then you can enter schedule generation commands:

30 10 * * * /home/likegeeks/Desktop/myscript
Thanks to this command, the script will be called every day at 10:30. If you encounter the "Resource temporarily unavailable" error, run the command below as root:

$ rm -f /var/run/crond.pid
You can organize periodic launch of scripts using cron even easier by using several special directories:

/etc/cron.hourly /etc/cron.daily /etc/cron.weekly /etc/cron.monthly
Placing a script file in one of them will cause it to run hourly, daily, weekly or monthly, respectively.

Run scripts on login and shell startup

You can automate the launch of scripts based on various events, such as user login or shell launch. You can read about files that are processed in such situations. For example, these are the following files:

$HOME/.bash_profile $HOME/.bash_login $HOME/.profile
To run a script on login, place its call in the .bash_profile file.

What about running scripts when you open a terminal? The .bashrc file will help you organize this.

Results

Today we discussed issues related to management life cycle scripts, we talked about how to run scripts in the background, how to schedule their execution. Next time, read about functions in bash scripts and library development.

Dear readers! Do you use tools to schedule command line scripts to run on a schedule? If yes, please tell us about them.



tell friends