This is a tutorial about how to create simple batch scripts in linux console. I will use “/home/” as my root folder. It can be applied on any folder.
First, I’ll Create a file known as “mybatch”, if the file does not already exist. If the file already exists the accessed / modification time is updated for the file mybatch
touch /home/mybatch
Then, give the created file permissions to write to it, and modify its content, through the famous chmod.
For permissions information: I suggest to See help about chmod.
chmod 755 /home/mybatch
Finally, edit our file. I had a problem with my web server running Nginx and FastCGI, so I needed to stop both services, kill all remaining running processes, then start them back on.
I use VI tool for editing in linux.
vi /home/mybatch
A little about VI command:
- Press i to edit current line
- Press Esc to exit editing mode
- Press dd to delete current line
- Press ZZ to Save changes and exit
- Type :q! to exit Without saving changes
Terminate session:
- Use command: ZZ
Save changes and quit. - Use command line: “:wq”
Save (write) changes and quit. - Use command line: “:w”
Save (write) changes without quitting. - Use command line: “:q!”
Ignore changes and quit. No changes from last write will be saved. - Use command line: “:qa”
Quit all files opened.
Remember to hit “Esc” to exit editing mode anytime. Use it before executing any of above commands.
Here is a great start to Know more about VI command
A sample of my batch script:
- /usr/local/etc/rc.d/nginx stop
- /usr/local/etc/rc.d/php.cgi.sh stop
- killall -9 php-cgi
- /usr/local/etc/rc.d/php.cgi.sh start
- /usr/local/etc/rc.d/nginx start
Screenshot of VI command |
Don’t forget about user permissions for running your commands.