Can't start Ruby Sinatra App as systemd unit - gem not found - ruby

I'm trying to start Sinatra application as Systemd unit.
It's not a first time, this service was working before, but now I'm stuck.
systemctl start service always throws cannot find gem sinatra
Here is the log:
Nov 27 19:52:45 cloud.onehostcloud.hosting ruby[29074]: /usr/local/rvm/rubies/ruby-2.5.7-devel/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- sinatra (LoadError)
Nov 27 19:52:45 cloud.onehostcloud.hosting ruby[29074]: from /usr/local/rvm/rubies/ruby-2.5.7-devel/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in `require'
Nov 27 19:52:45 cloud.onehostcloud.hosting ruby[29074]: from /usr/lib/app/server.rb:34:in `<main>'
Nov 27 19:52:45 cloud.onehostcloud.hosting systemd[1]: app.service: main process exited, code=exited, status=1/FAILURE
The app.service file:
[Unit]
Description=API Server Service
[Service]
Type=simple
Group=appuser
User=appuser
ExecStart=/usr/bin/ruby /usr/lib/app/server.rb
StartLimitInterval=30
StartLimitBurst=3
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
I've checked and the $: is correct, so if I try with the same path all gems can be found and included via irb
Using CentOS 7 with RVM Ruby 2.5.7
Thank you!

Your reference to /usr/bin/ruby is bypassing RVM, and it sounds like you are using RVM to manage your gems.
RVM doesn't seem to have documentation on how to set up systemd services, but it does have guidance on launching scripts from cron, which is a similar situation.
Basically, RVM provides a wrapper for each combination of ruby version and gemset, which you can use in your app.service file. Your ExecService line would look something like this:
[Service]
ExecStart=/home/appuser/.rvm/wrappers/ruby-3.0.0#appgemset/ruby /usr/lib/app/server.rb
If you haven't created a specific gemset for your app, then you can omit the #appgemset part, and only specify the ruby version you want to use.

Related

Ruby on rails problem with mysql2 when creating linux service

I have installed ruby2.7. When i try to run my code from command line using. But when i create a service by creating a file /lib/systemd/system/abc.service. when i start this service using command systemctl start abc it gives me an error.
Oct 19 05:21:31 localhost fever-log: /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- mysql2 (LoadError)
I am stuck with this error why its giving error when i try to run the same code from service.

Jekyll: Operation not permitted # apply2files

I am using Windows 10's Linux subsystem where I installed Jekyll but when I run bundle exec jekyll serve it gives me:
jekyll 3.8.6 | Error: Operation not permitted # apply2files - /mnt/e/Work/project/_site/images/banner.jpg
I know there are some incompatibilities with Windows and Linux permissions, because all my files are owned by root for some reason, but there is a fix to this issue?
I found an answer here https://scottdorman.blog/2019/02/27/running-jekyll-on-wsl/ where the author says I have to close and reopen the bash, but it doesn't work.
These "operation not permitted" errors seem to be related to user rights or ownership.
I have seen strange errors on colleagues' MAC computers. The command ls -al showed that certain folders were owned by root.
If this applies to you, try to change/transfer ownership to your user with these commands:
sudo chmod -R 777 /mnt/e/Work/project/
sudo chown -R youruser /mnt/e/Work/project/
The difference between chmod and chown is simple:
chmod changes modes
chown changes ownership
Note: This is explained in detail on unixtutorial.org.
For the Windows Linux Subsystem, I have found this old blogpost devblogs.microsoft.com from January 2018:
How did permissions work in the past?
Prior to Build 17063, all files/folders list “root” as the owner and belonged to the group “root”. The permission bits on each file/folder was derived from Windows permissions–no write bit checked for Windows meant no write bit set in WSL.
Additionally, attempting to chmod or chown on a file/folder resulted in a no-op (they wouldn’t do anything!)
How do permissions work now?
For files that don’t have metadata, we apply the same approach as what is described in pre-17063 builds. But now, chmod/chown can assign metadata to the file or folder. Newly created files in WSL will be created with metadata by default and will respect the mount options you’ve set (discussed later) or the permissions you pass when executing a mkdir/open.
And, I have found this SO post:
chmod WSL (Bash) doesn't work, which states that it's enough to unmount and re-mount the drive to make chmod and chown work (after the mentioned WSL update).
As #Mike Cole mentioned above in the comments: If you are using wsl this can happen. I resolved this by restarting my machine and magically it works now.
The problem
This error also occoured for me on WSL. But I'm happy to have found a fix that doesn't require me to manually chmod permissions into directories. The error is verbose enough to tell me its an issue with my chmod command not allowing me to do that, rather than something I should be doing for every single folder on my system each time I want to rbenv into it to work on something like Jekyll.
ion not permitted # apply2files - /mnt/e/source/repos/work/ecs-saber/docs (Errno::EPERM)
Little research
EPERM is a validation check, a deep dive would reveal this is a safety feature that prevents you, even if you have access, from being able to do anyhting to file permissions with Ruby on directories / files that you are not a part of the group ownership on.
So how did I fix it?
I fixed it via adding WSL DrvFs speclines to my /etc/wsl.conf file (see: https://learn.microsoft.com/en-us/windows/wsl/wsl-config#wslconfig )
The rest of this document breaks down the steps.
1. Configure your WSL root account:
You should have a root password, because you won't have access to /etc, even with your user's sudo command, so first:
me#PRIVATE-PC:/mnt/c/Users/private$ sudo passwd
[sudo] password for me:
New password:
Retype new password:
passwd: password updated successfully
Now login as root
su -
Password:
Welcome to Ubuntu 20.04.5 LTS (GNU/Linux *.*.*.*-microsoft-standard-WSL2 x86_64)
...
This message is shown once a day. To disable it please create the
/root/.hushlogin file.
root#PRIVATE-PC:~# _
Now, good little admins should also harden their root accounts, but this isn't the scope of this answer. You can move forward for now, but you should go back and do this. I am obligated to tell you that about root access, anywhere, at any time (that includes at 3AM, Junior Devs, where the greatest mistakes happen with the root shell is when we are tired)
Put a string on your finger to harden root shell -- Circulation is important, don't actually do this physically, I need you to keep your fingers.
2. Apply /etc/wsl.conf change
Now that you are root, you can give the /etc partiiton the fix:
Write /etc/wsl.conf, just copy/paste on command line and press [enter]:
cat <<'EOF' > /etc/wsl.conf
[automount]
enabled = true
root = /mnt
options = "metadata,uid=1000,gid=1000,umask=022,case=off"
mountFsTab = false
EOF
Why umask=022 ?
Octal permissions to exclude (octal subtract) from files and directories. This produces a permission set of 0755 where your user has the ability to read, write, and execute, but your overlaying group and everyone can only read and execute.
3. Restarting without rebooting
Now, do a flip. (Simulate a restart of WSL) See: https://superuser.com/questions/1126721/rebooting-ubuntu-on-windows-without-rebooting-windows
I'll improve the solution presented by saying you don't have to reboot, if you know the right commands.
root#PRIVATE-PC:~# exit
logout
me#PRIVATE-PC:/mnt/c/Users/private$ exit
ohmyPOSH> wsl -d Ubuntu-20.04 --shutdown
ohmyPOSH> wsl -t Ubuntu-20.04
ohmyPOSH> Restart-Service LxssManager
Just count to ten in your head (Microsoft's stated rule is 8 seconds), get up have a stretch, then log back in via Windows Terminal PowerShell tab:
ohmyPOSH> wsl -d Ubuntu-20.04 -u me
Check the permissions on your jekyll project folder:
me#PRIVATE-PC:/mnt/c/users/me$ cd /mnt/e/source/repos/work/ecs-saber/docs
me#PRIVATE-PC:/mnt/e/source/repos/work/ecs-saber/docs$ ls -lah
drwxr-xr-x 1 me me 4.0K Sep 1 04:20 .
drwxr-xr-x 1 me me 4.0K Sep 1 03:49 ..
-rwxr-xr-x 1 me me 56 Sep 1 04:20 .gitignore
-rwxr-xr-x 1 me me 6 Sep 1 03:50 .ruby-version
-rwxr-xr-x 1 me me 419 Sep 1 04:20 404.html
-rwxr-xr-x 1 me me 2.1K Sep 1 04:20 _config.yml
drwxr-xr-x 1 me me 4.0K Sep 1 04:20 _posts
And now they are owned by your user with permissions set to a much less unnerving 0755! You no longer have to do this for every single directory, in fact, if you check others, like your user folder:
me#PRIVATE-PC:/mnt/c/users/me$ ls -lah
drwxr-xr-x 1 me me 512 Sep 1 03:59 .
drwxr-xr-x 1 me me 512 Aug 28 04:45 ..
drwxr-xr-x 1 me me 512 Aug 30 07:08 .aws
drwxr-xr-x 1 me me 512 Aug 30 07:08 .azure
drwxr-xr-x 1 me me 512 Aug 30 10:03 .cache
drwxr-xr-x 1 me me 512 Aug 30 09:53 .conda
-rwxr-xr-x 1 me me 4 Aug 31 07:32 .condarc
drwxr-xr-x 1 me me 512 Aug 28 12:19 .docker
...
You'll have a lot of the same.
Onward
me#PRIVATE-PC:/mnt/e/source/repos/work/ecs-saber/docs$ jekyll new . --force
Running bundle install in /mnt/e/source/repos/work/ecs-saber/docs...
Bundler: Fetching gem metadata from https://rubygems.org/............
Bundler: Resolving dependencies...
Bundler: Using public_suffix 5.0.0
Bundler: Using bundler 2.3.21
Bundler: Using colorator 1.1.0
Bundler: Using concurrent-ruby 1.1.10
Bundler: Using eventmachine 1.2.7
Bundler: Using http_parser.rb 0.8.0
Bundler: Using ffi 1.15.5
Bundler: Using forwardable-extended 2.6.0
Bundler: Using rexml 3.2.5
Bundler: Using rb-fsevent 0.11.2
Bundler: Using liquid 4.0.3
Bundler: Using mercenary 0.4.0
Bundler: Using rouge 3.30.0
Bundler: Using unicode-display_width 1.8.0
Bundler: Using safe_yaml 1.0.5
Bundler: Using addressable 2.8.1
Bundler: Using i18n 1.12.0
Bundler: Using sassc 2.4.0
Bundler: Using rb-inotify 0.10.1
Bundler: Using pathutil 0.16.2
Bundler: Using em-websocket 0.5.3
Bundler: Using terminal-table 2.0.0
Bundler: Using jekyll-sass-converter 2.2.0
Bundler: Using kramdown 2.4.0
Bundler: Using listen 3.7.1
Bundler: Using kramdown-parser-gfm 1.1.0
Bundler: Using jekyll-watch 2.2.1
Bundler: Using jekyll 4.2.2
Bundler: Fetching jekyll-seo-tag 2.8.0
Bundler: Fetching jekyll-feed 0.16.0
Bundler: Installing jekyll-feed 0.16.0
Bundler: Installing jekyll-seo-tag 2.8.0
Bundler: Fetching minima 2.5.1
Bundler: Installing minima 2.5.1
Bundler: Bundle complete! 7 Gemfile dependencies, 31 gems now installed.
Bundler: Use `bundle info [gemname]` to see where a bundled gem is installed.
New jekyll site installed in /mnt/e/source/repos/work/ecs-saber/docs
🎉 No more EPERM validation failures.

create Scheduled Task from working Ruby script on Ubuntu inside Windows10

I have a Windows10 Ubuntu bash environment set up. It has a few rubygems installed (without docker, bundler, without rvm, and also this is not rails). The script is working fine when I run it manually from inside the Ubuntu terminal, in the user home folder ~/
Now my goal is create a Windows Scheduled Task using the Task Scheduler application, to run the script daily, like I would with a crontab in Unix.
The action in this Scheduled Task is the challenge. In testing manually from the Windows CMD prompt, I got as far as:
C:\>C:\Windows\System32\bash.exe -c "ruby ~/myscript.rb"
That fails :
Traceback (most recent call last):
2: from /home/lam/ruby/remind_prepare_dad_before_iterative_ends.rb:15:in `<main>'
1: from /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
/usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require': cannot load such file -- tiny_tds (LoadError)
Above error tells me it cannot find my gem, TinyTDS. What do I add to the command line to make it find my gems?
I found a crude workaround for my own problem: just install the gem from the same command line:
C:\>C:\Windows\System32\bash.exe -c "sudo gem install tiny_tds"
I had to re-install a few more gems I use in the same way.
After this, I put in a -C argument to start ruby in the right directory, like so:
C:\>C:\Windows\System32\bash.exe -c "ruby -C ~/ruby myscript.rb"
works like a charm.
I suppose now I have duplicated gems floating in spaces on the machine, but we will address that issue if it ever becomes a problem. :-)
Try bundle exec ruby ~/myscript.rb

FCGI Ruby Gem not found when using apache

I am using Apache with FastCGI to run a ruby application. I have installed the apache fcgi module and also the Ruby fcgi gem.
When I run the fcgi script 'search.fcgi' like so
ruby search.fcgi
It runs successfully. However when starting Apache I get the following error in my log file when it tries to run the same script:
/usr/local/rvm/rubies/ruby-2.1.8/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- fcgi (LoadError)
from /usr/local/rvm/rubies/ruby-2.1.8/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /home/user/fcgi/search.fcgi:13:in `<main>'
Line 13 in search.fcgi is simply
require 'fcgi'
It appears as though when running through Apache it is unable to detect the installed gem. I have loaded irb and and run require 'fcgi' which returns true.
Am I missing something here? Some path or config item I need to set?
My problem was caused by the fact that Ruby was installed using RVM. There was some issue with the script from apache accessing some parts of the RVM instillation. The solution was to remove the RVM ruby install and reinstall ruby from source.

Could not find 'passenger' when trying to install passenger-install-nginx-module

I'm getting the same error every time I'm trying to install the rvmsudo passenger-install-nginx-module:
~/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/dependency.rb:296:in `to_specs': Could not find 'passenger' (>= 0) among 114 total gem(s) (Gem::LoadError)
from ~/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/dependency.rb:307:in `to_spec'
from ~/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_gem.rb:47:in `gem'
from ~/.rvm/gems/ruby-2.0.0-p247#rails4/bin/passenger:22:in `<main>'
from ~/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `eval'
from ~/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `<main>'
Sys. info:
OSX 10.0.4
ruby 2.0.0-p247
rails 4
passenger 4.0.20 (installed with rvmsudo)
P.S.
I already used rvmsudo, so just using this doesn't solve the problem as some other questions/answers could propose. I think the problem is in paths, but where?
Update:
I did the same without rvmsudo (install the passenger gem), but still running the (rvmsudo) passenger-install-nginx-module causes the error with missing gem passenger
Update2:
I installed homebrew and with its help installed passenger. Then installed passenger nginx module which didn't work:
Unable to start the Phusion Passenger watchdog because its executable (/usr/lib/phusion-passenger/agents/PassengerWatchdog) does not exist. This probably means that your Phusion Passenger installation is broken or incomplete, or that your 'PassengerRoot' directive is set to the wrong value. Please reinstall Phusion Passenger or fix your 'PassengerRoot' directive, whichever is applicable.
Then I installed the passenger nginx module with rvmsudo passenger-install-nginx-module, so now it worked. I configured it for my rails app. But if I start nginx from /opt/nginx/sbin/nginx and navigate to the vhost to my rails application I get:
No data received
Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data.
If I look to the /opt/nginx/log/error.log I get for that request:
libc++abi.dylib: terminate called throwing an exception
2013/10/21 15:15:30 [alert] 54636#0: worker process 54723 exited on signal 6
What could be here a problem, is it passenger? My rails app worked with WebRick well, and it runs on another server also good with passenger. Maybe it is already another question...
try
brew install nginx --with-passenger
Phusion Passenger author here. First of all, I apologize for the problems. We're doing our best to make the installation experience as problem-free as possible.
From the information you've provided, I'm not entirely sure what went wrong. I'd like to go over this with you, so that we automatically check for (and prevent) these kinds of problems in the future. If you have time today, could you join the IRC channel #passenger on irc.freenode.net? My time zone is Europe/Amsterdam.

Resources