Change Ruby Process Name in OSx - ruby

I am trying to give my Ruby program a different name. I am running this on OSx with Ruby version 2.1.2-p95. I am looking in the Activity Monitor which I believe uses top, but I am not 100% sure.
I have tried $0 = "My process name", $0 = "My process name\0", $PROGRAM_NAME = "My process name", $0 = "my_process_name". None of which seem to do the trick.
I have also tried:
require "fiddle"
def set_process_name(name)
Fiddle::Function.new(
DL::Handle["prctl"], [
Fiddle::TYPE_INT, Fiddle::TYPE_VOIDP,
Fiddle::TYPE_LONG, Fiddle::TYPE_LONG,
Fiddle::TYPE_LONG
], Fiddle::TYPE_INT
).call(15, name, 0, 0, 0)
end
set_process_name("My process name")
I would love to have a cross-platform way to do this, but I'm mainly after an OSx way right now.
Similar question without satisfactory answer:
Change the ruby process name in top

I have not found a cross platform way to do it. The closest thing is using Process.setproctitle but on OSX it just works on the command line, not in the Activity Monitor app.
For OSX you will need bundling your script within an App and set the process name on the Info.plist file

Related

How to deal with shell commands that never stops

Here is the case;
There is this app called "termux" on android which allows me to use a terminal on android, and one of the addons are androids API's like sensors, tts engines, etc.
I wanted to make a script in ruby using this app, specifically this api, but there is a catch:
The script:
require('json')
JSON.parse(%x'termux-sensor -s "BMI160 Gyro" -n 1')
-s = Name or partially the name of the sensor
-n = Count of times the command will run
returns me:
{
"BMI160 Gyroscope" => {
"values" => [
-0.03...,
0.00...,
1.54...
]
}
}
I didn't copied and pasted the values, but that's not the point, the point is that this command takes almost a full second the load, but there is a way to "make it faster"
If I use the argument "-d" and not use "-n", I can specify the time in milliseconds to delay between data being sent in STDOUT, it also takes a full second to load, but when it loads, the delay works like charm
And since I didn't specify a 'n' number of times, it never stops, and there is the problem
How can I retrieve the data continuously in ruby??
I thought about using another thread so it won't stop my program, but how can I tell ruby to return the last X lines of the STDOUT from a command that hasn't and will not ever stop since "%x'command'" in ruby waits for a return?
If I understood you need to connect to stdout from a long running process.
see if this works for your scenario using IO.popen:
# by running this program
# and open another terminal
# and start writing some data into data.txt
# you will see it appearing in this program output
# $ date >> data.txt
io_obj = IO.popen('tail -f ./data.txt')
while !io_obj.eof?
puts io_obj.readline
end
I found out a built in module that saved me called PTY and the spawn#method plus thread management helped me to keep a variable updated with the command values each time the command outputted new bytes

Command Line Radio Buttons/Options in Ruby

How do we make a command line options like this:
I would image the code would look like this
options = Hash.new()
options['Monolithic'] = 'Monolithic application'
options['Microservice'] = 'Microservice application'
options['Gateway'] = 'Microservice gateway'
puts 'Which *type* of application would you like to create?'
options.each do |key, option|
puts option
end
# interface here
A menu in a console windows can be done with several gems, the best known are curses, tty-prompt and derivates and Highlight
If you want a simple graphical menu see my answer here.
See here for an example of what curses can do.
Here are more examples.
Result will depend on OS and console being used.
I wrote tty-prompt gem to help build interactive menus. Implementation of the example would look like:
require "tty-prompt"
prompt = TTY::Prompt.new
type = prompt.decorate("*type*", :yellow)
prompt.select("Which #{type} of application would you like to create?") do |menu|
menu.choice "Monolithic application", "Monolithic"
menu.choice "Microservice application", "Microservice"
menu.choice "Microservice gateway", "Gateway"
end
The above will render the following select menu in the console:
This gem is tested to work on a variety of operating systems and there are many types of prompts available.

Chef cookbook - copy file to local user with '~' ruby variable

Well, let's start by saying I'm a chef noob and I am trying to hash this code out.
I am in a full mac shop. I am using Chef to automate system wide changes. As I'm new, I'm rolling it out onto our Mac AV systems.
Basically, there is a folder on a file server that has MAC SCREEN SAVERS directory. I copy the server directory locally to the MAC OS X /User/user_name/Pictures directory.
So, this is what I got in chef:
local_folder_modified = File.mtime("~/Pictures/SCREEN SAVER NEW MACS")
server_folder_modified = File.mtime("/Volumes/SERVER/SCREEN\ SAVER\ NEW\ MACS/")
if server_folder_modified != local_folder_modified
# file has changed
then
require 'fileutils'
FileUtils.cd('server_folder_modified') do
FileUtils.rm('local_folder_modified/*')
FileUtils.cp_r './*', 'local_folder_modified'
Else
end
end
Anyways, I can't figure how to set the '~' to be the running user of this recipe. So, if Comp_A has user Jim_Beam and Comp_B has user Jack_Daniels, I don't want to set the code to be:
ENV[HOME] = /user/jimbeam
As it won't work on Jack_Daniels. Right?
I've read that file.expand will work, or ENV, but I am really unsure what will be the best code to say
"hey, I want the current user that will need this screen saver - so set the environment as a variable so it works across different nodes".
Anyways, thanks for your help. I hope I am making sense!
Yes, use File.expand. It will expand the tilde ~ to be the the home directory of the user running this cookbook. Alternatively, you could do:
"#{ENV['HOME']}/Pictures/SCREEN SAVER NEW MACS"
Like the previous comment, this is not chef DSL or ruby code. What is the source of this code or is it just pseudo-code to ask the question?
Also, chef-client is not frequently run as multiple users in a chef server deployment. It's usually run in a sudo context. So maybe you are referring to a --local-mode or chef-zero application?
You may want to use file stat of /dev/console to get the current user. Depending how you are running the chef-client Env[‘Home’] might not give you want you want. Try this:
console_user = Etc.getpwuid(::File.stat("/dev/console").uid).name
home_dir = ::File.join(‘Users’, console_user)
You can see that the chef launchd provider uses this method to determine the console user
Also there is a much simpler way to do what you are trying to accomplish with the remote_file resource. Try this:
console_user = Etc.getpwuid(::File.stat("/dev/console").uid).name
home_dir = ::File.join(‘Users’, console_user)
pics = ::File.join("#{home_dir}/Pictures/")
server_base_url = "https://PLACE_WHERE_STORE/Wallpapers")
[
‘Pic1’,
‘Pic2’,
].each do |pic|
remote_file ::File.join(pics, pic) do
source “#{server_base_url}/#{pic}”
owner console_user
group console_user
mode '0755'
action :create
end
end
For added security you should also include checksum

Startup Items - Creating a basic one

Please save me from a potential nervous breakdown!
I've been following Apples documentation (see below) on how to create a Startup Item. Currently I'm just trying to get my script to print something to the console, much less actually run my app.
Here are my two scripts, one is the startup executable, the other is the plist:
#!/bin/sh
. /etc/rc.common
# The start subroutine
StartService() {
# Insert your start command below. For example:
echo "hey Eric we've started"
# End example.
}
# The stop subroutine
StopService() {
# Insert your stop command(s) below. For example:
echo "STOPPED ERIC"
# End example.
}
# The restart subroutine
RestartService() {
# Insert your start command below. For example:
echo "RESTART ERIC"
# End example.
}
RunService "$1"
{
Description = "Software Update service";
Provides = ("SoftwareUpdateServer");
Requires = ("Network");
Uses = ("Network");
OrderPreference = "Late";
Messages =
{
start = "Starting Software Update service";
stop = "Stopping Software Update service";
};
}
Using terminal I tried to set the permissions as closely as possible as to how it is documented in the example in the link below. The odd thing was that the files didn't show the 'root' aspect to their ownership.
I then ran SystemStarter start theApp and nothing happens. Absolutely nothing.
Any help?
http://developer.apple.com/mac/library/documentation/MacOSX/Conceptual/BPSystemStartup/Articles/StartupItems.html
You should not create a Startup Item any more. It's a legacy mechanism and superseded by launchd. Write a plist for launchd instead. I know this is not the help you wanted, but sadly with Apple, you need to follow the mothership...
Read this section of the same document instead.
See the document just you quoted yourself:
Note: The launchd facility is he preferred mechanism for launching daemons in Mac OS X v10.4 and higher. Unless your software requires compatibility with Mac OS X v10.3 or earlier, you should use the launchd facility instead of writing a startup item. For more information, see “Guidelines for Creating and Launching Daemons.”
Note that v10.4 become available in 2005.

How to build a top-like UI in Ruby

I want to build an application with a text based UI that is similar to the Linux command 'top', in Ruby. What toolkits and/or techniques can I use to build the UI? In particular I want an area of the console window that is constantly updating, and the ability to press keys to manipulate the display.
For a terminal interface, see http://ncurses-ruby.berlios.de/
Ncurses is great for console apps and you can find bindings for it for lots of languages (or just use shell scripting). There's even a cursed gtk (http://zemljanka.sourceforge.net/cursed/) though I think work on it stopped quite awhile back.
You didn't mention your platform, but for OS/X there's a great little app called Geektool (http://projects.tynsoe.org/en/geektool/) which allows you to put script output on your desktop. I use a small ruby script to generate my top processes list:
puts %x{uptime}
IO.popen("ps aruxl") { |readme|
pslist = readme.to_a
pslist.shift # remove header line
pslist.each_with_index { |i,index|
ps = i.split
psh = { user: ps[0], pid: ps[1], pcpu: ps[2], pmem: ps[3],
vsz: ps[4], rss: ps[5], tty: ps[6], stat: ps[7],
time: ps[8], uid: ps[9], ppid: ps[10], cpu: ps[11], pri: ps[12],
nice: ps[13], wchan: ps[14], cmd: ps[16..ps.size].join(" ") }
printf("%-6s %-6s %-6s %s", "PID:", "%CPU:", "%Mem", "Command\n") if index == 0
printf("%-6d %-6.1f %-6.1f %s\n",
psh[:pid].to_i, psh[:pcpu].to_f, psh[:pmem].to_f, psh[:cmd]) if index < 10
}
}
(This could probably be better, but it was the first ruby script I ever wrote and since it works I've never revisited it to improve it - and it doesn't take input. Anyway, it might help give you some ideas)
there's stfl but i don't know if the ruby bindings are maintained. ncurses is probably your best bet.

Resources