2

I have finished my project and I need a file to run 3 commands in terminal.

php artisan serve
php artisan migrate
php artisan queue:work --daemon --timeout=

and I need to receive database name,username,password and change .env file

This is my bash shell so far

#!/bin/bash
echo "Server Ready"
sudo php artisan serve;
echo "Migration Started";
sudo php artisan migrate;
echo "migration Finished Successfuly";
echo "Queue Started";
sudo php artisan queue:work --daemon --timeout=3000;

but I need help to make it fully work.

I run this script and after first command php artisan serve my script stopped

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
hosein in jast
  • 370
  • 2
  • 15
  • What do you mean by "stopped"? Is there any error message given? Additionally, the given shell script does not contain that mentioned first command, you only use that command using `sudo` - any reason for this? – Nico Haase Jan 22 '20 at 13:20
  • 1
    It should be noted that `php artisan serve` should **never** be used in production. It can only handle one concurrent request, and is intended only for development/testing. – ceejayoz Jan 22 '20 at 13:59

3 Answers3

3

i found my awnser

#!/bin/bash

cp .env.example .env

# config name database
sed -i -e 's/DB_DATABASE=laravel//g' .env
echo -n "Enter a database name > "
read database
sed  -i "12i  DB_DATABASE=$database" .env

# config username
sed -i -e 's/DB_USERNAME=root//g' .env
echo -n "Enter a  username > "
read username
sed  -i "12i  DB_DATABASE=$username" .env

# config password
sed -i -e 's/DB_PASSWORD=//g' .env
echo -n "Enter  password > "
read password
sed  -i "12i  DB_DATABASE=$password" .env

echo "Server Ready"
sudo php artisan serve &
echo "Migration Started" 
sudo php artisan migrate &
echo "migration Finished Successfuly" 
echo "Queue Started"
sudo php artisan queue:work --daemon --timeout=3000 &

i post it for who need it in line 1 rename the .env.example to .env after that rename the database with user selected name and do it for username and password and thank to my friend @Lajos Arpad in last line we can run php artisan commands all together

Nicolas
  • 8,077
  • 4
  • 21
  • 51
hosein in jast
  • 370
  • 2
  • 15
2

The php artisan serve command is running an internal php webserver. Which means it needs to block the current thread to listen to PHP input. You could simply run this command after all the other.

#!/bin/bash
echo "Migration Started";
sudo php artisan migrate;
echo "migration Finished Successfuly";
echo "Queue Started";
sudo php artisan queue:work --daemon --timeout=3000;
echo "Server Ready"
sudo php artisan serve;

But, as mention here you should have to use that command in order to have a running environment. You might want to use a WAMP server instead. Or maybe docker if you are feeling adventurous.

Nicolas
  • 8,077
  • 4
  • 21
  • 51
  • in second command i will stop again like artisan serve queue stop the script – hosein in jast Jan 22 '20 at 13:06
  • 1
    @hoseininjast I've updated my question. For your .env file, it should matter because the code is retreiving environnement variables, and those variables are accessible from everywhere. Still, you should use `php artisan serve`. – Nicolas Jan 22 '20 at 13:08
  • 1
    I'm not sure what your `php artisan queue:work` command does, but if it blocks the thread. i suggest running `php artisan serve` in another terminal. – Nicolas Jan 22 '20 at 13:09
  • i want to give this file to my friend and when he run that artisan migrate tables and queue startet for listening to jobs and artisan for start local server – hosein in jast Jan 22 '20 at 13:14
  • 1
    there are [ways to use multithreading in bash](https://stackoverflow.com/a/2425914/5784924). You could look into that. – Nicolas Jan 22 '20 at 13:17
2

You just need to put & at the end of each line which would be a blocker and you do not intend to wait for it:

#!/bin/bash
echo "Server Ready"
sudo php artisan serve &
echo "Migration Started" 
sudo php artisan migrate &
echo "migration Finished Successfuly" 
echo "Queue Started"
sudo php artisan queue:work --daemon --timeout=3000 &

As about your .env file you can implement a script in PHP that gets command line inputs and changes .env accordingly and then call this with whatever inputs you need.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175