Linux shell command source execute only with . call - shell

On my ubuntu 18 server i create an .sh file like this one:
#!/bin/bash
source "/home/ubuntu/.venv36/bin/activate"
nohup python manage.py runserver &
well, i save it as startup.sh in my /var/www/web/core directory.
At this point if in console I put my working directory like this:
cd /var/www/web/core
and execute
. startup.sh
all work fine, virtualenv get created and nohup start runserver.
the problem is if from root i call directly my script like this:
/var/www/web/core/startup.sh
command source does not run, but nohup yes, and finnaly my app does not work.
How can I execute directly my sh file? I need to execute it at server startup, now I use in crontab the command:
#reboot /var/www/web/core/startup.sh
but it does not work like I explain above.

Related

Execute symfony command in bash script

I can't get to execute symfony command in bash script when I run it in cron.
When I execute the .sh script by hand everything is working fine.
in my bash file the command is executed like this:
/usr/bin/php -q /var/www/pww24/bin/console pww24:import asari $office > /dev/null
I run the scripts from root, the cron is set to root as well. For the test i set files permissions to 777 and added +x for execution.
the bash script executes fine. It acts like it's skipping the command but from logs i can see that the code is executed
It turned out that symfony system variables that I have stored on server are not enough. When you start to execute the command from command line its fine, but when using Cron you need them in .env file. Turned out that in the proces of countinous integrations I only got .env.dist file and I've to make the .env file anyways.
Additionaly I've added two lines to cron:
PATH=~/bin:/usr/bin/:/bin
SHELL=/bin/bash
and run my command like this from the bash file:
sudo /usr/bin/php -q /var/www/pww24/bin/console pww24:import asari $office > /dev/null

How can I run command inside virtualenv in shell script

I develop my own django-based project with pipenv.
Couple days ago I created simple bash script to speed up boring stuff.
All time I run terminal, go to the same directory and execute pipenv shell, after that I open up project in VScode code . and started python manage.py runserver for looking up my progress.
I try create script which do the same stuff but simplied, just run webber and here go (it comes from /usr/local/bin).
But I have one problem of these, I can't keep my pipenv shell running and execute python manage.py runserver at the same time. I mean when I stop Ctrl+C python server I don't receive my virtual enviroment (this happed in bash script ~ normally work fine).
However, server is start up, so it's virtual env.
I tried with pipenv run command but it doesn't get inside virtual env at all.
Script:
#!/bin/bash
cd ~/Documents/myprojects/Webber
code .
source $(pipenv --venv)/bin/activate
python manage.py runserver
My question is: How can I run command inside virtualenv in shell script and receive this subshell?
you can use the full path to the virtualenv folder instead of pipenv command. For example, if you created the virtual environment in you home directory called venv-webber:
source $HOME/venv-webber/bin/activate

How to run set of terminal commands in a file one by one?

I was working on a Django project and each time I open the project, I have to run these three commands one by one.
source virtualenv/bin/activate (to activate virtual environment)
cd myproject (to enter project folder)
python3 manage.py runserver (to run the server)
Is there a way I can write these three commands in a text file (or some other file) so that a single command will run these three commands one by one?
Before running those three commands, terminal looks like this:
After running those three commands, the terminal should look like this:
Try this:
In file runscript.sh,
#!/bin/bash
source virtualenv/bin/activate
cd myproject
python3 manage.py runserver
Then run command: . runscript.sh instead of ./runscript.sh
Save following content to a file runserver.sh
#!/bin/bash
source virtualenv/bin/activate && cd myproject && python3 manage.py runserver
and
chmod 744 runserver.sh
And you can just run ./runserver.sh
&& will ensure previous command was successful before running next command. If any command fails to run, it will abort rest.

Can't start bash script on ubuntu docker container

it's been a few days now but I really can't understand how to run a bash script correctly in ubuntu/xenial64 using docker. Any clarifications will be very appreciated.
I created a Dockerfile like this
FROM ubuntu:16.04
COPY setup.sh /setup.sh
RUN chmod +x /setup.sh
ENTRYPOINT [ "/setup.sh" ]
The error returned is: standard_init_linux.go:195: exec user process caused "no such file or directory"
But why?? If I run ls the file is correctly placed on the root. I also tried using CMD ["/setup.sh"]. My script file has a shebang like this #!/bin/bash.

Can't run startup.sh as startup despite being installed in PATH

I'm running Ubuntu 14.04. I've created a script called startup.sh and given it execute permissions. I put it in my $HOME/bin folder, and I've checked and this is indeed on the PATH. I've rebooted my computer just to be sure. I am still unable to run startup.sh just as a command (typing startup on the command line). Am I wrong in what I've done or assumed is possible?
My end goal was to be able to just type on the command line "startup" and execute the script I created.
$ startup
startup: command not found
$ echo $PATH
/home/travis/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
$ cd /home/travis/bin && ls -l
total 4
-rwxrwxr-x 1 travis travis 803 Dec 16 10:08 startup.sh
I can still run the script by navigating to $HOME/bin and running bash startup.sh of course, but that wasn't the goal.
Setting executable permissions and a #!/bin/bash line per How do I run a shell script without using "sh" or "bash" commands? did not work for me as an answer, hence my confusion.
If your file is named startup.sh, then the command to run it needs to be startup.sh.
If you want the command to be startup, don't include any extension on your filename: Just name it startup.

Resources