Java Run Shell Script Example

How many of them do you use?

Keyboard

Photo by Troy Chen on Unsplash.

The ability to write effective shell scripts is a must-have in any programmer's toolbox because it makes everything so much easier.

It is also quite addictive. Once you get the hang of it, you start to automate a greater and greater chunk of your life until you become this guy.

However, in this article, we will not write shell scripts. Rather, we will look at the five most common ways of executing one. By most common, I mean most often used by me or people I know. If you know any other neat ways or tricks, make sure to drop them in the comments. My default shell is bash.

1. Run From a File

This is the easiest and most common way to execute a script. You simply write your script and then run it by typing its name in the command line.

To be able to do this, the folder where your script is stored has to be discoverable by the system, so you have to add it to the $PATH variable. By convention, this folder is called bin. Here is how you'd add it to $PATH:

            export PATH="$HOME/bin:$PATH"          

But what if you want to store your script in some other folder? No need to worry. You can easily create a symbolic link between your script and the bin folder so it will be discoverable wherever it's stored. Let's say you're in some folder that's not bin, and you want to store your script called cool_script there. You can use the following command:

            ln -s cool_script ~/bin/cool_script          

One last important thing is to add executable permission to your script with the following command:

            chmod u+x cool_script          

2. Run With the Specified Interpreter

This one method is really short and simple. This doesn't even require you to set a shebang at the start of your script. Or if you did set the shebang and specify some other interpreter, it will be run with that irrespective of the shebang.

If you have a script called simple.sh located in your root directory, you can run it with bash by:

            bash ~/simple.sh          

Or if you prefer the zsh:

            zsh ~/simple.sh          

I prefer the first method, but there might be some cases where you need to do this too.

3. Run in the Background and the nohup Command

Let's say you ssh into a remote server to do some work and start some long-running process. If you were to abort your connection now, the job would get killed and the work would never get done. Here is where the nohup command is very useful.

When the connection to the remote terminal hangs up, the OS sends the SIGHUP message to all the programs that were launched from that terminal. The program then either stops execution immediately or is built to catch the SIGHUP message and continue running or do some cleanup before exiting.

The first option for running something in the background is to use the $ sign. This puts the process into the list of background jobs (which can be listed with jobs -l). However, when the shell is closed, it still receives the SIGHUP message.

The second option is to use the disown command. This removes the job from the shell's job list. One of the implications is that it will not receive the SIGHUP command. We're getting closer, but if we terminate the ssh connection, for example, our program will fail as soon as it will try to read from stdin or write to stdout.

The third and ideal option is to use the nohup command. This baby does three important things that result in basically separating the process from the shell:

  • Close stdin.
  • Redirect stdout and stderr to a file called nohup.out.
  • Make the process not receive SIGHUP.

We have to pair it with $ to have it run in the background, which is not the default mode. So if we want to run cool_script this way, for example:

            nohup cool_script $          

4. The exec Command

When you run a script the regular way, the shell spawns a new subprocess. This is called forking. The exec command is used to prevent this. It allows you to completely replace the current shell with the command following exec. The current process is destroyed and no subshell is forked. For example:

            exec cool_script          

So, why would we want to use this? I don't use exec in every script I write, but it can be useful from time to time.

Let's say you're on a Mac when the default shell is zsh, but you want to do some work in bash. You could simply run exec bash to change the default in-memory shell to bash.

Another useful thing is to replace the default bash shell for the restricted shell because maybe you want to control what users can do on your system. Then simply run exec rbash and you're done.

5. Schedule a Script To Run at a Specific Time

The simplest and easiest way to run a script later is to make use of the at command. Let's say you want to run cool_script at 4 p.m. today. Simply do this (assuming the script is located at the root):

            at -f ~/cool_script teatime          

Instead of teatime, you can specify whatever you want, like now + 2 minutes, tomorrow, or 15:08.

The second option is to use the crontab tool. If you would like to run cool_script every day at 4 p.m., then you would do crontab -e to edit your crontab file or create one if it doesn't exist. Then you write your cron job like this:

            0 16 * * * /path/to/cool_script          

The third option, which only applies if you're on a Mac, is to make use of launchd, which is a really nice tool for automating scripts.

Let's start by creating a .plist file at ~/Library/LaunchAgents:

            touch ~/Library/LaunchAgents/com.schedule-test.daemon.plist          

Now open this file with your editor of choice and add the following fields to it:

This creates a configuration for running our script every 60 seconds, as defined in StartInterval. RunAtLoad means it will run the tasks as soon as the job definition is loaded.

To start the job run:

            launchctl load ~/Library/LaunchAgents/com.schedule-test.daemon.plist          

And to stop it:

            launchctl unload ~/Library/LaunchAgents/com.schedule-test.daemon.plist          

If you want to schedule your job to run on specific dates, you can insert something like this into your .plist file:

            <key>StartCalendarInterval</key>
<dict>
<key>Minute</key>
<integer>42</integer>
<key>Hour</key>
<integer>13</integer>
<key>Weekday</key>
<integer>2</integer>
</dict>

This will run your script every Tuesday at 1:42 p.m. Cool, isn't it?

Conclusion

In this article, we looked at what I think are the five most common ways to execute shell scripts. Thanks for taking the time to read this. I hope you gained some new insight or value from it.

swiftvance1996.blogspot.com

Source: https://betterprogramming.pub/5-ways-to-run-a-shell-script-7829fc80b49c

0 Response to "Java Run Shell Script Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel