Process Management
Process Management and Automating Tasks
A feature that is really awesome in Linux is the ability to schedule a command or task to run on a specified schedule. In this section, we will learn how Linux can run commands at a specific time using two different methods. The first method will have the command always run at that time, and in the second method, the command will run a single time. This is very useful when you want a task to always run at a specific time or to have a command run once in the future. We will also look at various ways to view and control processes in Linux. Understanding process management is important as a process is a fundamental part of an OS. Without the ability to create a process, a computer would be pretty useless.
ps
The ps (process status is what ps stands for) command will display the current running process. This is a very powerful command as it can display a information about all processes on your Linux system and who the owner is. When you use the ps command think of this as a picture of your Linux system processes at that exact moment. It will not auto update, so if you want to see process information again you will have to reissue the command.
kill & killall
The kill and killall command will stop a process from running. With the kill command you need to provide the PID (process ID) and with killall you just need to provide the command you wish to stop running. These are both very useful commands to terminate a process that might have ended up in an unknown state or that is not responding.
Putting it together: ps & kill
Here is a quick demo of using ps and the kill command to stop a process from running. The ps command is used to find the PID and then we use kill to terminate that process.
top
The top command provides you with a real time and interactive display of your processes. Within the top command you can also terminate a process. I like to use this command when trying to determine if a certain process is consuming a large amount of resources on my Linux system as it lets me view stats in real time.
https://linux.die.net/man/1/top
https://www.howtogeek.com/668986/how-to-use-the-linux-top-command-and-understand-its-output/
cronjobs & crontab
Both cronjobs and crontab are ways that you can have a command or a script run at a specific time. For example, you can have a script run every Sunday at 5 pm, or you could have a script run every 10 minutes if you wish. The crontab is a table of cron jobs that are scheduled to run.
Please make sure to read the two following links about cronjobs and crontab.
https://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
https://devconnected.com/cron-jobs-and-crontab-on-linux-explained/
Sometimes, getting the timing correct or ensuring that your cronjob will run when you expect it is not easy. Fortunately, someone created an awesome site to help understand exactly when a cronjob will run.
at
The at command provides the ability to run a command once in the future. This can be useful for testing or making sure a task is completed at a certain time. For example, you might want to reboot a server during off hours and you don't want to wake up or go to the office at 3 am to do it, so you can use the at command to reboot it for you. Though, if the server doesn't reboot as expected that could be a problem.
jobs & fg & bg
These three commands are tied together. The jobs command can list processes and their current state. You can use the fg command to move a process that is running in the background to the foreground. The bg command moves a suspended process to running in the background.
https://man7.org/linux/man-pages/man1/jobs.1p.html
https://man7.org/linux/man-pages/man1/fg.1p.html
https://man7.org/linux/man-pages/man1/bg.1p.html
https://www.cyberciti.biz/faq/unix-linux-jobs-command-examples-usage-syntax/