LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to start command script in system start up ubuntu (https://www.linuxquestions.org/questions/linux-general-1/how-to-start-command-script-in-system-start-up-ubuntu-4175508367/)

mahi_nix 06-18-2014 07:21 AM

How to start command script in system start up ubuntu
 
Hi Guys,

I have ubuntu 12.04 server which has ruby site hosted on apache. now i must have to run one command in my site's root directory to start my sites as that command start ruby process. Please find the command details as below.
Quote:

bundle exec unicorn -l 3030 -E production -D
I must have to run this command to start my web portal in the web portals DocumentROOT with the specific user only.

can anyone suggest me how can i run this command at the system startup with the specific user and in DocumentROOT of the web portal.

I tried so many things and google out but no luck.

Please help me to sorted out the issue.

Regards,
Mahipal

pan64 06-18-2014 08:33 AM

what have you tried exactly? you can add start scripts in the rcX.d dirs.

mahi_nix 06-18-2014 08:48 AM

Hi Pan64,

I tried to add that script in rcX.d but it dont works. I wanted to run that command script on specific directory (i.e. /home/rails/www.abc.com/" with user RAILS.

Regards,
Mahi_nix

pan64 06-18-2014 08:49 AM

which script?

mahi_nix 06-19-2014 01:12 AM

Hi Pan64,

Please find the command script as below:

Quote:

#!/bin/sh

bundle exec unicorn -l 3030 -E production -D
I have named this script as start.sh and i am running this script in my Documentroot os the site lets say in "/var/www/www.abc.com" directory by switching with the user RAILS.

Regards,
Mahi_nix

pan64 06-19-2014 01:38 AM

where did you put that start.sh? how did you set the permissions?

mahi_nix 06-19-2014 02:59 AM

I have kept that scirpt in "/var/www/www.abc.com" directory, and the permission on script is 775.

I am starting this script by following below steps:

1) su - rails
2) cd /var/www/www.abc.com
3) ./start.sh

This way i am manually starting this script.

now i wanted to start this automatically at the boot time.

pan64 06-19-2014 03:16 AM

you wrote:
Quote:

I tried to add that script in rcX.d but it dont works
. How did you try it?

mahi_nix 06-19-2014 03:31 AM

I added that script in /etc/init.d/ and then created symlink in rcX.d (i.e S20rubustart) and then reboot server to test it.

Regards,

pan64 06-19-2014 05:46 AM

probably S99 would be better.
you need to combine all the steps in #7 into one single script I think.
that will look like:
Code:

cd /var/www/www.abc.com
/usr/bin/sudo -u rails /full/path/to/bundle exec unicorn -l 3030 -E production -D


mahi_nix 06-19-2014 07:03 AM

Hi,

I tried solution suggested by you but it didnt worked. Please find the error as below.

Code:

root@localhost:~# ./start.sh
Gemfile syntax error:
/var/www/www.abc.com/Gemfile:36: syntax error, unexpected ':', expecting $end
gem 'xcases', git: 'ssh://user1@git.gitserv.com/var/lib/git/xcases.git'

I executed this script using root user login.

Please suggest.

Regards,

mahi_nix 06-19-2014 07:06 AM

I can only execute the start.sh script in "/var/www/www.abc.com" directory with rails user. script does not work with any other user and on any other location on server.

Regards,

bathory 06-19-2014 07:20 AM

Quote:

Originally Posted by mahi_nix (Post 5190371)
I have kept that scirpt in "/var/www/www.abc.com" directory, and the permission on script is 775.

I am starting this script by following below steps:

1) su - rails
2) cd /var/www/www.abc.com
3) ./start.sh

This way i am manually starting this script.

now i wanted to start this automatically at the boot time.

You can use /etc/rc.local, by adding the sequence of the commands above as follows:
Code:

su - rails -c "/var/www/www.abc.com/start.sh"

mahi_nix 06-20-2014 12:26 AM

Hi bathory,

I tried your suggested solution but it is not working.

Regards,

bathory 06-20-2014 01:42 AM

Quote:

Originally Posted by mahi_nix (Post 5190977)
Hi bathory,

I tried your suggested solution but it is not working.

Regards,

What exactly is not working? The command or the startup method (/etc/rc.local)?

mahi_nix 06-20-2014 02:15 AM

Hi,

Command is not working. can you help me to create init script for this same process or command.

Regards,
Mahipal

bathory 06-20-2014 02:52 AM

Quote:

Originally Posted by mahi_nix (Post 5191020)
Hi,

Command is not working. can you help me to create init script for this same process or command.

Regards,
Mahipal

Doh, it should work! What it the error you get?
You can also use any of the following 2 approaches:
1. Just put the command "/var/www/www.abc.com/start.sh" in a script and then use su to run it:
Code:

su - rails -c "/path/to/script"
2.
Code:

sudo -u rails /var/www/www.abc.com/start.sh

mahi_nix 06-20-2014 02:59 AM

Hi Bathory,

I cant see any error message as it is running during the system startup and i dont have the console access of the server. i am accessing my server remotely using SSH.

Let me try the above solution. I get back to you soon.

Regards,
Mahipal

mahi_nix 06-20-2014 03:06 AM

Hi bathory,

below is the error after applying your suggested solution.
Quote:

root@localhost:~# sudo -u rails /var/www/www.abc.com/start.sh
Could not locate Gemfile
Regards,

pan64 06-20-2014 03:14 AM

I assume the cd into /var/www/www.abc.com is missing.

mahi_nix 06-20-2014 05:35 AM

Hi,

cd is not missing. I have in the script.

can you tell me how can i make a script which do the following steps.

1) su to rail (su - rail).
2) then cd to /var/www/www.abc.com (cd /var/www/www.abc.com).
3) then execute the scirpt ./start.sh

can you tell me how can i make a script. might be that will resolve my issue.

Regards,

bathory 06-20-2014 06:33 AM

Create the script:
Code:

#!/bin/sh
cd /var/www/www.abc.com
./start.sh

Run it as the user rails:
Code:

su - rails -c "/path/to/script"
Try it from CLI first to see if it works, or it fails and for what reason

mahi_nix 06-20-2014 07:10 AM

Hi,

below is the error.
Quote:

rails@localhost:~$ ./test.sh
Could not find rake-10.3.1 in any of the sources
Run `bundle install` to install missing gems.
I executed the script with rails user.

below is the another result of the command.
Quote:

su - rails -c /home/rails/test.sh
Gemfile syntax error:
/var/www/www.abc.com/Gemfile:36: syntax error, unexpected ':', expecting $end
gem 'xcases', git: 'ssh://user1@git.gitserver.com/var/lib/git/xcases.git'
Please suggest

Regards,

bathory 06-20-2014 07:27 AM

Quote:

below is the error.

rails@localhost:~$ ./test.sh
Could not find rake-10.3.1 in any of the sources
Run `bundle install` to install missing gems.
I'm not familiar with rails, but from the error above, looks like something is missing.
So as suggested, i.e. run "bundle install" and see what you get

mahi_nix 06-20-2014 07:54 AM

Hi bathory,

I am running this script from rails user's home directory, If i run the script command in /var/www/www.abc.com directory it will work as all the bundle components are installed in /var/www/www.abc.com directory. so i must have to run ./start.sh script in the said directory. I cant installed bundle components in the rails user's home directory.

Regards,

bathory 06-20-2014 08:14 AM

Quote:

Originally Posted by mahi_nix (Post 5191189)
Hi bathory,

I am running this script from rails user's home directory, If i run the script command in /var/www/www.abc.com directory it will work as all the bundle components are installed in /var/www/www.abc.com directory. so i must have to run ./start.sh script in the said directory. I cant installed bundle components in the rails user's home directory.

Regards,

This is what the script of post #22 does:
Changes directory to /var/www/www.abc.com
Runs the ./start.sh
Then you, as root use su or sudo to execute the script with the rails user rights

mahi_nix 06-20-2014 08:51 AM

Hi,

But I tried that way same as post:22 which also not works. I tried the same way suggested in post 22.

Regards,


All times are GMT -5. The time now is 11:35 PM.