Linux world is full of fun, the more we go in depth, you will find more interesting things. We will try to give you some tips so that you and others are different, here is what we prepared three small skills.
1. How does the case of using the task scheduler Cron under Linux in
In Linux, a task scheduler / command called Cron. When we need to schedule a task, we will use Cron, but you know we're not used Cron also can schedule a task to run in the future of time? You can follow the following suggested actions ......
Run a command every 5 seconds (date) and then writes the results to a file (data.txt). To achieve this, we can run the following one-line script directly from the command prompt.
$ Whiletrue; do date >> date.txt; sleep 5; done &
Above script explanation:
while true: let the script enters a loop condition is always true, that is manufacturing a dead cycle, repeated over and over again inside the command to run.
do: do while statement is a keyword, the command will be executed after it, it can be placed behind one or a series of commands.
date >> date.txt: Run date command and writes its output to the data.txt file. Note that we use >> instead of>.
>>: File (date.txt) additions to the write operation, so that after each run command, the output will be appended to the file. If> then this will overwrite the previous content over and over again.
sleep 5: the script to sleep in five seconds, and then run the command later. Note that the only time unit in seconds. This means that if you want the command to run once every 6 minutes, you should use the sleep 360.
done: while loop end of the block tag.
&: The entire process into the background.
Similarly, we can run any script. The example below is run every 100 seconds a script called script_name.sh of.
Also worth mentioning is the script file mentioned above must be in the current directory, or need to use the full path (/home/$USER/.../script_name.sh). One-line script to achieve the above functions as follows:
$ Whiletrue; do / bin / sh script_name.sh; sleep 100; done &
Summary: The above is not a one-line script Cron alternatives because Cron tool supports many options, more flexibility, customization is also higher. However, if we want to run some tests, such as I / O evaluation, said one-line script is also useful.
2. How does the use of clear command to clear the contents of the terminal
How do you empty the contents of the terminal? You might think this is a silly question. Well, we all know you can use the clear command. If you develop using ctrl + l shortcuts habit, we will save a lot of time.
Ctrl + l effect shortcut and clear commands. So the next time you can use ctrl + l to empty the contents of the terminal.
Summary: Because ctrl + l is a shortcut, we can not use in the script. So if we need to empty the contents of the screen in the script, or the need to use the clear command. But I can think of all the other cases, ctrl + l are more effective.
3. Run a command in a different directory, then automatically return to the current working directory
This is a lot of people may not know the amazing skills. You might want to run any command in a different directory, then back to the current directory. To achieve this purpose, you only need to put a command in parentheses.
Let's look at an example:
avi @ deb: ~ $ (cd / home / avi / Downloads /)
avi @ deb: ~
It will first cd to the Downloads directory, then back to the previous home directory. Maybe you think the inside of the command is not executed, or that something is wrong, because from the command prompt do not see any change. Let's look at a simple modification of this order:
avi @ deb: ~ $ (cd / home / avi / Downloads / && ls -l)
-rw-r ----- 1 avi avi 54272May318: 37 text1.txt
-rw-r ----- 1 avi avi 54272May318: 37 text2.txt
-rw-r ----- 1 avi avi 54272May318: 37 text3.txt
avi @ deb: ~ $
In the above command, it first entered the Downloads directory, then list the contents of the file, and finally back to the current directory. And it proves that the command executed successfully. You can include any command in parentheses, will be executed after the successful return to the current directory.