How To Run a Script In Linux
H ow do I run a Linux shell script? How can I run a script in Linux operating system using command line options?
By default, the shell script will not run. You need to set execute permission for your shell script. To execute or run script type the following command:
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | None |
Est. reading time | N/A |
chmod +x script-name-here
OR
chmod 0755 script.sh
Next, use the ls command to view permission on the script:
$ ls -l script-name-here
To execute the script, type:
$ ./script-name-here
You can also run a script using any one of the following syntax:
$ /path/to/shell/script/backup.sh
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Run a script called backup.ksh using ksh shell:
$ ksh backup.ksh
To run a script called backup.bash using BASH shell:
$ bash backup.bash
Examples that shows how to run a script in Linux
Create a shell script called hello.sh using a text editor such as vi or gedit/nano:
nano hello.sh
OR
vim hello.sh
Append the following code:
Conclusion
You learned how to write a simple shell script and run a script in Linux operating system with help of chmod and other commands. Please see the following tutorials for more information on bash shell scripting under Linux or Unix-like operating systems:
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Category | List of Unix and Linux commands |
---|---|
Documentation | help • mandb • man • pinfo |
Disk space analyzers | df • duf • ncdu • pydf |
File Management | cat • cp • less • mkdir • more • tree |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Linux Desktop Apps | Skype • Spotify • VLC 3 |
Modern utilities | bat • exa |
Network Utilities | NetHogs • dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
Searching | ag • grep • whereis • which |
Shell builtins | compgen • echo • printf |
Text processing | cut • rev |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
this is very good. It’s just missing one thing. the export command should be added in in the
/.bashrc file to survive across sessions.
$ tail -1
or the equivalent for your chosen shell (bs, ksh, tcsh etc …)
Just the solution I needed to figure this out, thanks man! 😀
Nice.
For compiling C prgms,
cc hello.c
./a.out
btw, Why don’t you have categories in WP?
Click on category icon (hint: Bash shell).
Hello Vivek, I was not aware that that image is a category link, too 🙂 It really looks like a post image. I’m referencing this site from time to time, and I’ve learned that you have categories just now! I’d really suggest using a different method for showing categories, unless you want to hide them 🙂 Thanks for the great site.
we can run a script even with out permissions using source command
sourch scriptname.sh
no dear if u want to run script the u hv to give execute permission on that file
like….scriptname.sh
chmod 755 or 777 scriptname.sh
Don’t be a patronizing shit…
hi. i m facing problem to write for setup ip address ,subnet and gateway in linux
kindly suggest me how i do…
if u have the give me idea…
#include
int main()
<
system(“c:\\windows\\your system name\\ip config”);
return 0;
>
Good information. I am trying to run a sql using a shell command.
Please provide the info..
How to run the Unix command only during the even and odd hours only.
Kindly provide the command to incorporate in the script.
Hi,
I am having a problem related to this. I have downloaded an program and it has a GUI written in Java. I need to run the script to launch this program, with “sudo” privileges in order to having it running properly. I don’t want to navigate to the folder where this script is, every time I need it, therefore I first made it executable and added the folder to the PATH. Now, when I write “sudo script-name”, I get “script-name not command found”, if I write only “script-name”, it finds it but it doesn’t run properly. Is there a way to launch a script, that is in the PATH, with sudo privileges? Thank you in advance.
Run it as follows:
Or cd to /home/foo and run:
Thank you. I decide to add it as an alias: “alias script-name=’sudo bash /path/to/script/script-name”
What’s the difference with §. /PATH/TO/TARGET§ and §./PATH/TO/TARGET§ ?
thanx, im learning and your are a good teacher.
Источник
How to measure time of program execution and store that inside a variable
In order to find out how long certain operations within a Bash (v4+) script take, I would like to parse the output from the time command «separately» and (ultimately) capture it within a Bash variable ( let VARNAME=. ).
Now, I am using time -f ‘%e’ . (or rather command time -f ‘%e’ . because of the Bash built-in), but since I already redirect the output of the executed command I’m really lost as to how I would go about to capture the output of the time command. Basically the problem here is to separate the output of time from the output of the executed command(s).
What I want is the functionality of counting the amount of time in seconds (integers) between starting a command and its completion. It doesn’t have to be the time command or the respective built-in.
Edit: given the two useful answers below, I wanted to add two clarifications.
- I do not want to throw away the output of the executed command, but it will not really matter whether it ends up on stdout or stderr.
- I would prefer a direct approach over an indirect one (i.e. catching output directly as opposed to store it in intermediate files).
The solution using date so far comes closes to what I want.
9 Answers 9
To get the output of time into a var use the following:
You can also just ask for a single time type, e.g. utime:
To get the time you can also use date +%s.%N , so take it before and after execution and calculate the diff:
In bash, the output of the time construct goes to its standard error, and you can redirect the standard error of the pipeline it affects. So let’s start with a command that writes to its output and error streamas: sh -c ‘echo out; echo 1>&2 err’ . In order not to mix up the command’s error stream with the output from time , we can temporarily divert the command’s error stream to a different file descriptor:
This writes out to fd 1, err to fd 3, and the times to fd 2:
It would be more pleasant to have err on fd 2 and the times on fd 3, so we swap them, which is cumbersome because there’s no direct way to swap two file descriptors:
This shows how you can postprocess the output of the command, but if you want to capture both the output of the command and its times, you need to work harder. Using a temporary file is one solution. In fact, it’s the only reliable solution if you need to capture both the command’s standard error and its standard output. But otherwise, you can capture the whole output and take advantage of the fact that time has a predictable format (if you use time -p to get the POSIX format or the bash-specific TIMEFORMAT variable).
If you only care about wall clock time, running date before and after is a simple solution (if slightly more imprecise due to the extra time spent loading the external command).
Источник
How do you time how long a command took to run? [duplicate]
How would you find out how long a running process took to complete?
^This example only has 1 second of resolution.
Any shell is acceptable.
1 Answer 1
time will execute the rest of the command line as a command (in this example longrunningcommand —takeyourtime ) and when the command is done it will print the elapsed time.
example output (bash builtin time )
the interesting information is real 0m5,020s . that means the command took about 5 seconds. for more information about the other numbers see here: https://stackoverflow.com/questions/556405/what-do-real-user-and-sys-mean-in-the-output-of-time1
time is a builtin command in most shells. the example output above is from the bash builtin. sometimes you will want to use the «system time». for example to avoid spawning a shell just for the time.
if you want to use the system time do it like this:
the backslash works in bash and maybe some other shells. command works in most shells. /usr/bin/time should work always.
example output using system time :
the interesting information is 0:06.02elapsed . that means the command took about 6 seconds. for the meaning of the other numbers read the man page of time : http://man7.org/linux/man-pages/man1/time.1.html
you can change the output of the system time . use -p to get output similar to the shell builtin time .
use -f to write your own format. %E is the «elapsed» part. that is the part you are usually most interested in.
the difference of the shell builtin and the system command is not just the format of the output. for more information the difference read here: What is the difference between a builtin command and one that is not?
how to redirect or capture the output
for demonstration observe command hellostdoutstderr :
capture stdout and stderr separately
the system time prints to stderr so it is captured in the stderr redirect
you can tell the system time to print to a separate file
the bash builtin time always prints to the terminal even if stdout and stderr is redirected. this is possible because it is a builtin and can do whatever it likes (in the shell)
(stdout and stderr are captured but time can still print to shell)
to time more complex commands you have severals options
if you just want to time two command one after the other
also works with pipe
for more complex stuff
but mind the quoting and other shenanigans. better put the commands in a script file:
Источник
How to run a script at a certain time on Linux? [closed]
Want to improve this question? Update the question so it’s on-topic for Stack Overflow.
Closed 8 years ago .
I have a text file containing a specific date and time. I want to be able to run a script at the time specified in that file. How would you achieve that? Create another script that runs in background (sort of a deamon) and checks every second if the current time is matching the time in the file? Is there another way? The machine is a linux server , Debian wheezy. Thanks in advance
4 Answers 4
Look at the following:
This code line executes «ls -l» at a specific time. This is an example of executing something (a command in my example) at a specific time. «at» is the command you were really looking for. You can read the specifications here:
The at command exists specifically for this purpose (unlike cron which is intended for scheduling recurring tasks).
Cron is good for something that will run periodically, like every Saturday at 4am. There’s also anacron, which works around power shutdowns, sleeps, and whatnot. As well as at.
But for a one-off solution, that doesn’t require root or anything, you can just use date to compute the seconds-since-epoch of the target time as well as the present time, then use expr to find the difference, and sleep that many seconds.
Usually in Linux you use crontab for this kind of scduled tasks. But you have to specify the time when you «setup the timer» — so if you want it to be configurable in the file itself, you will have to create some mechanism to do that.
But in general, you would use for example:
Would execute the script every Friday at 1:30 (AM) Here:
next 2 *’s are day of month and month (in that order) and 5 is weekday
Источник