Ruby Trouble Interacting with Filesystem when run from LaunchAgent - ruby

I have a Ruby script that I am triggering with an OS X launchagent. The script reads a bunch of files (21 total) from disk, does some processing, compiles them into an RSS feed, and uploads it to a remote machine using scp. The script works fine when I run it from the command line. However, when I run it with a launchagent I run into some very strange issues. I have isolated the problem to this line:
match = #content.match(/(?<=^ID: )\d+/)
#content here refers to the contents of a file, read in earlier. This line is embedded in a class which serially processes each file. Something about this line is somehow causing execution to be dropped-- when I put a log statement after this line, it is triggered only for 16/21 of the files when running as launchagent. When running from the command line, there is no issue.
I also have noticed that these 5 files are almost, but not exactly, the five largest of the 21 files. What could possibly be going on? Any well-known typical issues when working with LaunchAgents?
UPDATE: this is one of the files that is breaking the code.

Related

Creating Directory in Shell Causes Last Folder to have Extra "\r"

I'm writing a basic shell script that creates directories and changes directories to that folder to write in it.
Right now I have the following:
mkdir -p "Home/first/second/third"
cd "Home/first/second/third"
This produces undesired results as the file structure looks as follows:
- Home
- first
- second
- third\r
I'm looking to get rid of the \r on the third\r but am unsure why it is doing so. I've tried a ton of different variations and can't seem to figure out what the issue is with adding that extra \r
Are you sure that's the exact script?
Because the mkdir -p "Home/first/second/third" should and does create a structure like.
Home
- first
- second
- third
You do need to clarify what OS and shell you are using.
Did you create the file on the host or copy it to the host from a windows machine?
-EDIT
Use
dos2unix script.sh
to get rid of those pesky \r characters that windows put in.
Credit to #Klox for figuring this out.
Files edited on a Windows computer end in \r\n, while files edited on Linux computers end in \n.
Therefore, despite the code being compiled on Linux, it still ends created files with \r\n, causing the last directory created in a mkdir to end with \r. In order to remove this \r, files must be written in the linux environment.

GCP SDK gsutil rsynch not returning progress

When I run gsutil rsynch from the GCP Console, or a .bat file, the full progress data does not display (it used to I'm pretty sure.) I'm on vers 403.0.0
Here is the command:
>gsutil rsync -r -n \\xxxx\WEBSITE\xxx\pages gs://xxx/pages
Building synchronization state...
Starting synchronization...
>
If I run the same command, followed by a pause in a .bat file, the pause is not hit - the batch file terminates. glist (maybe others) do output their data to the console and continue on to the balance of the batch commands.
c:
cd "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\bin\"
gsutil rsync -r -d \\xxx\pages gs://xxx/pages
pause <<<<< never get here
If I use .Net Process I can capture the Standard Out which does contained the progress data. However StdOut seems to close well after rsynch has finished.
Is this a bug? Or am I missing something?
I recommend that you update to the latest gsutil release.
I tested on my machine and also on the Cloud Shell, both with the rsync version 4.31 and they display the progress of the command, listing the files being copied.
I reproduced the script on different environments and observed that the script skips the commands after the rsync complete only on Windows machines. I tested on Google Cloud SDK Shell and also on Cygwin for Windows.
However, on a Linux machine and also on Cloud Shell, the same script works as expected and it executes the subsequent commands after the rsync completes.
This behavior depends on the implementation of individual shells. You will need to catch the unexpected behavior and handle the situation in the desired way, as the solution differs depending on the environment.

Bash script behaving differently for different files

I have a bash script that uses awk to process some files that I have downloaded. If I run the script on any of the files it does not work properly. However, if I transfer the contents of a file in a newly created one it seems to work as supposed. Could it have anything to do with the settings of the files?
I have two files file hotel_12313.dat and hotel_99999.dat . The first one is downloaded and the second one is created by me. If I copy the data from the first file into the second one and I execute the script on both of them the output is different.

Running a command before every execution of a script/program

I'm using Bash on Windows and what I'm missing is a good IDE. I've tried running a GUI app with the following tutorial but it doesn't work well every time and it's frustrating.
What I want is to run the script that would copy the files from a folder on Windows into a folder on Unix subsystem, but only the files that are different. And same for the other direction (if I change something from terminal, to be updated in the Windows folder). I want that script to be run every time I call ./SOME_EXECUTABLE in that folder. For the check weather the file was changed or not I can use hg status because I'm mostly working with Mercurial.
Is there a way to do this without making a separate shell script that would combine those calls? Something like a macro.
You could use a function in .bashrc to achieve your requirement, and run your required script that copies stuff from across machines as you needed. Assuming you have the script in place, lets say, e.g. copyScript.sh you can add a function like
function copyOnExecute() {
./copyScript.sh
./EXECUTABLE
}
This way you can call the function copyOnExecute, every time you want to run your executable.
You could add an alias to .bash_aliases such as:
alias execute="./copyScript.sh && ./$1"
You can replace ./ with your scripts path
This runs the executable after your script has finished, and only if it finished successfuly.
It would be easier for you then writing a function in one of the .rc files
If you'd write a function called execute, in the future you might forget where It was written, using an alias helps you avoid this.

OS X - How do you stop a bash script from running on startup?

I recently downloaded Ruby Switcher in an attempt to switch back to Ruby from JRuby. I eventually switched back to normal Ruby by other means so I deleted ruby_switcher.rb as it was of no further use to me. Now, at every startup I get this very annoying message in my terminal:
-bash: /Users/ethanwilkins/ruby_switcher.sh: No such file or directory
How can I stop this message from appearing without restoring the file?
It's probably getting called in your profile file.
Usually, the file is .bash_profile and is located in your home dir.
After reading what bash says about startup files, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.

Resources