How to write a script in vim.
Vim is one of the most powerful text editors in linux/unix. But it has a unique control scheme that is bewildering for most users. But there is a way to create text notes and scripts in vim.
First, open notepad and a linux terminal.
Second,on the linux terminal type "view [scriptname].sh" or "view [scriptname].py
Third, type your bash or python script on notepad
The next step is to copy everything you typed on notepad by highliugiting every thing you yped by hoilding down the left mouse button and scrolling down, and then right clicking on the highlighted section and click copy.
Then click on the terminal and right click on it. What you copied on the notepad will be there on on the terminal.
Then type in :wq! To save what you typed on vim.
It's worth noting that once you create a file, the permissions will default to chmod 644 (and for directories to chmod 755) So what we need to do is to change the file permissions where you can run or execute the script. To do so, type in "chmod 744 [scriptname].py or .sh" to make it an executiable file.
Finally, you can run the script by typing in ./[scriptname].sh or you can move the script to the crontab to run it automatically.
Adding a script via cron.You can then place the scripts to be ran automatically via the cron service. This is where all the automation of linux servers take place.
The crontab file can be found in /var/spool/cron/crontabs/ or /var/spool/cron.
An example of a cron tab entry is this
30 18 * * * rm /home/someuser/tmp/*
This means the temp file of someone's user will be removed every day at 6:30pm.
Cron entries look like the following:
* * * * * [commandor/script]
I will explain it further via the following:
(1) 2 3 4 5 [command script]
(1) represents the minute (0-60), 2 represents that hour (1-24, which is based on the international/military time system). And that for tasks that will be done daily.
(3) day you want the task to be done on a which day of the month (1-31), and (4) represents that certain month you want the task to be done (1-12).
(5) Represents the day of the week you want the task to be done (0 for sunday through 6 for Saturday)
Also you can use @reboot before the actual script you type into the file to have the script or command run when it finishes rebooting.
To actually edit the cronttab file enter crontab -e then type in the crontab entry as formatted above in the syntax.
Or even better:
You can easily add tasks to crontab by doing the following
[time syntax] [command or script] | crontab
To open the cron editor, or create one if it does not exist already enter the following
crontab -e
To list the tasks type in the following
crontab -l
to remove the crontab file type in
crontab -r