Last updated December 6th, 2023 00:23
If you manage a Linux server, you’re probably familiar with situations where you frequently use certain longer commands with specific parameters that you don’t want to repeatedly type. That’s why there’s a solution that allows you to create your own shortened command, which will not only be shorter but also easier to remember. As an example, let’s say I use my own command to display the last 10 lines of the Apache error log. I have only one website with WordPress on my server, and if something goes wrong, like misconfiguring the .htaccess file, I have my own command that shows me the recent lines of the Apache error log. Now let’s see how to create custom commands on Linux. It’s not difficult at all, and the process is relatively quick.
How to create custom commands on Linux
Start by creating a script
First, you’ll need a simple script. In my case, it will be a script for displaying the Apache error log, which will include this command:
tail -n 10 "$error_log"
The Bash script looks like this:
#!/bin/bash
error_log="/var/log/apache2/error.log" #Path to Apache error log
tail -n 10 "$error_log"
I saved this script in the /scripts directory as apache_error_log.sh. I will use this path and name in the example.
Associate the script with a custom command
I named my command “rozbitejapache”. It looks like this:
sudo rozbitejapache
Now you need to set it up so that when you enter the above command, the server will execute the script and perform the actions specified in the script. Follow these steps:
- Open the terminal on your Linux server and log in.
- Navigate to the directory with the Bash script using the command: cd /scripts.
- Check the permissions of the script using the command: ls -l apache_error_log.sh. The script should be executable. If not, adjust the permissions using the command: chmod +x apache_error_log.sh.
- Open the “/etc/sudoers” file for editing using the command: sudo visudo.
- Add a new line to the end of the /etc/sudoers file: <username> ALL=(ALL) NOPASSWD: /scripts/apache_error_log.sh. Replace <username> with your actual username on the server. This command allows the user to execute the script without entering a password. If you want to require a password, remove NOPASSWD: from the command.
- Save and close the /etc/sudoers file.
- Create a symbolic link to the script in the system command directory using the command: sudo ln -s /scripts/apache_error_log.sh /usr/local/bin/rozbitejapache. This creates a symbolic link named “rozbitejapache” in the “/usr/local/bin” directory that points to your script.
Now you should be able to run the command sudo rozbitejapache in your terminal and it will display the last 10 lines of the Apache error log.
* To understand my command – the word “rozbitejApache” in Czech means “brokenApache” in English.
How to create custom commands on Linux
Conclusion
By following this procedure, you have created your own command “brokenApache” that executes your Bash script for displaying the Apache error log. You can use the same process to create a script for any other command and create your own custom command that is less complicated to type or remember.
The website is created with care for the included information. I strive to provide high-quality and useful content that helps or inspires others. If you are satisfied with my work and would like to support me, you can do so through simple options.
Byl pro Vás tento článek užitečný?
Klikni na počet hvězd pro hlasování.
Průměrné hodnocení. 0 / 5. Počet hlasování: 0
Zatím nehodnoceno! Buďte první
Je mi líto, že pro Vás nebyl článek užitečný.
Jak mohu vylepšit článek?
Řekněte mi, jak jej mohu zlepšit.
Subscribe to the Newsletter
Stay informed! Join our newsletter subscription and be the first to receive the latest information directly to your email inbox. Follow updates, exclusive events, and inspiring content, all delivered straight to your email.