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


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 2 Weeks Ago
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 2 Weeks Ago
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:
LMHmedchem (2 Weeks Ago)
# 3  
Old 2 Weeks Ago
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 2 Weeks Ago
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:
LMHmedchem (2 Weeks Ago)
# 5  
Old 2 Weeks Ago
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 2 Weeks Ago
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 2 Weeks Ago
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 Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
If you run macOS High Sierra version 10.13.1, be sure to install today's update. Don Cragun OS X (Apple) 6 12-01-2017 01:05 PM
Capture power button press on MacOs High Sierra? xedge UNIX for Beginners Questions & Answers 0 10-12-2017 05:04 PM
[BASH] Script to manage background scripts (running, finished, exit code) sea Shell Programming and Scripting 17 10-18-2014 04:15 PM
Bash Question: HowTo Exit Script with User Input While Process is Running Mid-Loop? hilltop_yodeler Shell Programming and Scripting 9 03-06-2014 05:03 PM
[Solved] Rsh does not close connection on exit rbatte1 Shell Programming and Scripting 1 02-17-2014 07:53 AM
Close UNIX terminal bobbygsk UNIX for Advanced & Expert Users 11 10-04-2013 02:56 PM
How to exit from the parent script while the child is running? Little Shell Programming and Scripting 3 10-04-2013 11:33 AM
close a mac terminal cokedude UNIX for Advanced & Expert Users 1 12-21-2011 07:56 AM
SSH from MacOS X or PPC Debian to SUSE # Odd terminal chars # Eventual scripting pagrus UNIX for Dummies Questions & Answers 14 12-08-2011 12:01 AM
script does not close terminal after running Ditzyken Shell Programming and Scripting 3 09-01-2011 01:34 PM
Capturing the exit status of the script running in background paragkalra Shell Programming and Scripting 2 04-10-2010 04:22 PM
User cant log in (close terminal) lo-lp-kl AIX 2 02-23-2010 11:32 AM
Bash script to back up with rsync ajmoreti Shell Programming and Scripting 3 11-16-2009 05:36 AM
running terminal with script cdfd123 Shell Programming and Scripting 3 12-13-2008 02:35 AM
Running a script without a terminal session jjinno Shell Programming and Scripting 3 10-03-2007 01:50 AM