Terminal running bash/rsync script does not close with exit (MacOS High SIerra)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Terminal running bash/rsync script does not close with exit (MacOS High SIerra)
# 1  
Old 03-05-2019
Terminal running bash/rsync script does not close with exit (MacOS High SIerra)

Hello,

I am running a bash script to do an rsync back on a computer running MacOS High Sierra. This is the script I am using,
Code:
#!/bin/bash

# main backup location, trailing slash included
backup_loc="/Volumes/Archive_Volume/00_macos_backup/"

# generic backup function
   function backup {
     echo " " >> $1
     echo " " >> $1
     echo "***** new log entry *****" >> $1
     date >> $1
     rsync -v -a -b --suffix=.back -E --log-file=$1  $2  $3
   }

   echo "starting backup script"
   echo " "

#  backup /Users/my_user/ to /Archive_Volume/00_macos_backup/
   echo "backing up home"
   logfile_path=$backup_loc"/rsync_log/01_home_log.txt"
   backup_src_path="/Users/my_user"
   backup_destination_path=$backup_loc
   backup $logfile_path  $backup_src_path  $backup_destination_path
   
   echo " "
   echo "backup completed"
   echo ""
   echo " "
   echo -n "press enter to exit script"
   read exit_response
   exit

There are other calls to the backup function after the home backup. The script seems to work, but when I key enter, instead of the terminal window closing like it does in windows under cygwin, I get the following message,
Code:
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

[Process completed]

The terminal does not seem to function anymore but it does not close and I have to manually close it. It's not the end of the world but I don't understand why it doesn't close down on the exit command. Can anyone explain how to fix this?

LMHmedchem
# 2  
Old 03-06-2019
Hi LMHMedchem,
What initiates the backup script in the window it runs in on Windows under cygwin?

What initiates the backup script in the window it runs in on macOS?

How was the window in which the backup script runs on Windows under cygwin created?

How was the window in which the backup script runs on macOS created?
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 03-06-2019
Quote:
Originally Posted by Don Cragun
What initiates the backup script in the window it runs in on Windows under cygwin?
In windows, I perform, Send to > Desktop (create shortcut) on the script file, which lives in /cygwin/usr/local/bin. I then modify the properties of the shortcut to,
Code:
Target: C:\cygwin\bin\sh.exe /usr/local/bin/data_backup.sh
Start in: C:\cygwin\bin
Run: Normal Window

When I want to run the backup, I double-click on the desktop shortcut. I also have an entry for the shortcut in Task Scheduler so that it runs every night. I could schedule with cygwin cron, but the windows scheduler is already running anyway and I didn't see the need to start a second daemon for just one task. This doesn't always work in windows 7 for some reason. The task appears in the schedule as active, but the script never runs. I have a second shortcut that runs a backup to an external device. For that script, I have to enter the drive letter of the external device after it is attached. Both scripts have an option to shutdown the computer after the backup is finished.

Quote:
Originally Posted by Don Cragun
What initiates the backup script in the window it runs in on macOS?
The process is similar in MacOS. I create an alias of the script in the script directory. For the alias file, I right click > open with > other. Then I select "All Applications" from the drop down menu (instead of the default "Recommended Applications", and then select "Terminal", "always open with", and then "open". This opens the script via the alias and runs it, which is annoying. I have to comment out most of the script first, or sit through the backup. There doesn't seem to be an easy way to indicate "open with" without actually opening.

Once this is done, I move the alias to the desktop and change the icon to something appropriate. I can then run the script by double clicking on the icon. MacOS doesn't seem to have any kind of launcher like most Linux distros. Launchers had vanished from Linux in some places as well, which is the type of thing I just can't understand. Why remove a useful functionality and force users to launch applications the way some programmer somewhere thinks they should be launched? What possible harm could it do to just leave it for those who want to use it?

Quote:
Originally Posted by Don Cragun
How was the window in which the backup script runs on Windows under cygwin created?
How was the window in which the backup script runs on macOS created?[/QUOTE]I'm not really sure about this. I presume that the OS opens the terminal and runs the script in the terminal. In windows, there are some window options in the shortcut properties. There is nothing like this in MacOS. Since MacOS is using an alias, my guess it that Finder opens the terminal window.

I got an email from a friend who uses MacOS much more often than I do and he said that I may be able to adjust the preferences for Terminal so that windows close when the terminal completes whatever process it it running. I will check on that and report back.

LMHmedchem
# 4  
Old 03-06-2019
This is a Mac OS thing, not a bash thing. I'll take a look at mine when I get home tonight.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 03-06-2019
If (on macOS Mojave version 10.14.3) I:
  1. create an executable shell script,
  2. open the Finder application,
  3. in Finder go to the directory containing the script I just created,
  4. then right click on the name of that script, and
  5. select "Make Alias"
it creates a new file in the same directory with the name script alias (where script is the name of the file I right-clicked on in Finder) with the name of that file selected. I can then enter a new name for that alias or change the focus it I want to keep that name for the alias.

Note that I did not need to run the script to create the alias.

If I then double-click on the name of the alias in Finder, it opens a new window, runs the script in that window, and when the script exits, the window disappears.

If I move the file created onto my desktop and double-click on it there, macOS opens a new window, runs the script in that window, and when the script exits, the window disappears.

In either of the above cases, the window is running an interactive shell and can accept keyboard input in response to prompts issued by the script. If the script isn't expecting user input, I can minimize the window or move it behind other open windows and it will continue running until the script exits (and then the window disappears). Is that what you want to have happen?
# 6  
Old 03-06-2019
Quote:
Originally Posted by Don Cragun
If (on macOS Mojave version 10.14.3) I:
  1. create an executable shell script,
  2. open the Finder application,
  3. in Finder go to the directory containing the script I just created,
  4. then right click on the name of that script, and
  5. select "Make Alias"
it creates a new file in the same directory with the name script alias (where script is the name of the file I right-clicked on in Finder) with the name of that file selected. I can then enter a new name for that alias or change the focus it I want to keep that name for the alias.
This is the same process I used to create the alias (on MacOS 10.13.6, a bit behind yours).

Quote:
Originally Posted by Don Cragun
Note that I did not need to run the script to create the alias.
It was not creating the alias that required me to run the script. When I double click on my alias, the script opens in Text Editor instead of running in Terminal. Unlike a windows shortcut or Linux launcher, the alias doesn't have a property to let you specify the program you want to open the file in. In order to get the script to run in Terminal, I had to do the steps I described to define "open with" for the script file (not the alias file). This is the procedure where I had to actually run the script in order to specify the "open with" program. Once I had done that, I could double click on the script (or on the alias) and it would run in Terminal. I actually don't know if this now applies to all files on the system with the .sh extension, or just the one script file. The alias works in any location where it's located. I believe this is just a symbolic link like ln.

Quote:
Originally Posted by Don Cragun
If I then double-click on the name of the alias in Finder, it opens a new window, runs the script in that window, and when the script exits, the window disappears.

If I move the file created onto my desktop and double-click on it there, macOS opens a new window, runs the script in that window, and when the script exits, the window disappears.
After having set the "open with" value for the script file, when I double click on the alias file, a terminal window opens, the script runs, and finishes, but I get the [Process completed] message and the Terminal sits there in a non-functional state without the window closing.

Quote:
Originally Posted by Don Cragun
In either of the above cases, the window is running an interactive shell and can accept keyboard input in response to prompts issued by the script. If the script isn't expecting user input, I can minimize the window or move it behind other open windows and it will continue running until the script exits (and then the window disappears). Is that what you want to have happen?
This is what I want, I just don't know why the Terminal window closes when the script is finished on your system and stays open on mine. I have run this script on both windows and linux for a long time and I have never seen that before.

LMHmedchem
# 7  
Old 03-06-2019
Quote:
Originally Posted by LMHmedchem
This is the same process I used to create the alias (on MacOS 10.13.6, a bit behind yours).


It was not creating the alias that required me to run the script. When I double click on my alias, the script opens in Text Editor instead of running in Terminal. Unlike a windows shortcut or Linux launcher, the alias doesn't have a property to let you specify the program you want to open the file in. In order to get the script to run in Terminal, I had to do the steps I described to define "open with" for the script file (not the alias file). This is the procedure where I had to actually run the script in order to specify the "open with" program. Once I had done that, I could double click on the script (or on the alias) and it would run in Terminal. I actually don't know if this now applies to all files on the system with the .sh extension, or just the one script file. The alias works in any location where it's located. I believe this is just a symbolic link like ln.

After having set the "open with" value for the script file, when I double click on the alias file, a terminal window opens, the script runs, and finishes, but I get the [Process completed] message and the Terminal sits there in a non-functional state without the window closing.

This is what I want, I just don't know why the Terminal window closes when the script is finished on your system and stays open on mine. I have run this script on both windows and linux for a long time and I have never seen that before.

LMHmedchem
The fact that you have to take an intermediate step to specify what is to be used to run your script tells me that something is wrong. I'm guessing that you skipped part of the 1st step I mentioned in post #5. Please show us the output from the commands:
Code:
ls -l script
head script

where script is the name of the file containing your shell script.

If the mode of script doesn't allow you to execute it OR if the first line of script isn't #!/path/to/interpreter, then script is not an executable shell script file.

The link Finder is creating for me is not a hard link as would be created by ln nor a soft link as would be created by ln -s; it is a binary executable file that directly invokes terminal to run script or to run /path/to/interpreter /path/to/script.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with shutdown command in script (MacOS High Sierra)

Hello, I have a backup script that runs an rsync backup to an external drive. I use the script frequently on Windows and Linux and have installed it on a Mac. The script has an option to run shutdown after the backup has completed. Since backup can take hours to run, this is an option that is... (10 Replies)
Discussion started by: LMHmedchem
10 Replies

2. Shell Programming and Scripting

Issue with pwd for script run by double click on script (MacOS High SIerra)

Hello, I have the following script that just archives and clears some log files. #!/bin/bash # script: archive_logs_and_clear # add date to logfile names and copy archive directory # clear logs # change to script directory cd ... (4 Replies)
Discussion started by: LMHmedchem
4 Replies

3. OS X (Apple)

If you run macOS High Sierra version 10.13.1, be sure to install today's update.

Some hackers found a security hole in macOS High Sierra and tweeted it to the world before telling Apple about the problem. You can see the details from PC Magazine's daily news here: Apple Releases Fix for MacOS High Sierra 'Root' Bug. The original story this morning was published before a patch... (6 Replies)
Discussion started by: Don Cragun
6 Replies

4. UNIX for Beginners Questions & Answers

Capture power button press on MacOs High Sierra?

Hello everyone! I'm developing a MacOs Application in python and I'm having some issues trying to find information related to the power button pressed event. I know that in Ubuntu 14.04 you can find information about it on the acpi folders, but I realized that here in Mac that process is... (0 Replies)
Discussion started by: xedge
0 Replies

5. Shell Programming and Scripting

[BASH] Script to manage background scripts (running, finished, exit code)

Heyas, Since this question (similar) occur every now and then, and given the fact i was thinking about it just recently (1-2 weeks) anyway, i started to write something :p The last point for motivation was... (17 Replies)
Discussion started by: sea
17 Replies

6. Shell Programming and Scripting

Bash Question: HowTo Exit Script with User Input While Process is Running Mid-Loop?

Hi, I have written a script that allows me to repetitively play a music file $N times, which is specified through user input. However, if I want to exit the script before it has finished looping $N times, if I use CTRL+c, I have to CTRL+c however many times are left in order to complete the loop.... (9 Replies)
Discussion started by: hilltop_yodeler
9 Replies

7. Shell Programming and Scripting

[Solved] Rsh does not close connection on exit

Dear all, We have a service that we start up remotely with rsh but unfortunately, the rsh never returns to the calling server. This seems to be because the processes of the service we've just started hold the port open.RBATTE1 @ /home/RBATTE1>netstat -na|grep 49.51 tcp4 0 0 ... (1 Reply)
Discussion started by: rbatte1
1 Replies

8. UNIX for Advanced & Expert Users

close a mac terminal

Is there a trick to closing a mac terminal with a command? I would think you could just type exit into your terminal but that doesn't work. I also tried quit and close just for the hell of it and that didn't work either. Does anyone know what the command is? (1 Reply)
Discussion started by: cokedude
1 Replies

9. UNIX for Dummies Questions & Answers

SSH from MacOS X or PPC Debian to SUSE # Odd terminal chars # Eventual scripting

Well. I was recently given access to my work's machine via SSH. I'm pretty sure it's a SUSE machine, uname -a gives Linux machinename 2.6.16.60-0.54.5-bigsmp #1 SMP Fri Sep 4 01:28:03 UTC 2009 i686 i686 i386 GNU/Linux I'm not doing anything all that exciting, mostly data entry stuff. We... (14 Replies)
Discussion started by: pagrus
14 Replies

10. Shell Programming and Scripting

script does not close terminal after running

For a small script i want it so that the terminal closes when the script has completed its tasks. To do so i use at the end if the script the following: echo "Hello, World!" echo "Knowledge is power." echo "" echo "shutting down terminal in 10 seconds" exit 10 however the terminal stay's... (3 Replies)
Discussion started by: Ditzyken
3 Replies
Login or Register to Ask a Question