Automate file start

General support questions
jejedu67
Posts: 53
Joined: 2015/12/07 20:01:46

Automate file start

Post by jejedu67 » 2022/08/06 22:47:50

Hello,

On CentoS 7 I sometimes have to launch 2 files. To do this, I run the following 4 commands:

cd /home/gms_test
./GameManagerServer&
cd /home/zs_test
./ZoneServer&

I want to know if I can automate this every 15 minutes.

Thanks in advance

User avatar
TrevorH
Site Admin
Posts: 33202
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: Automate file start

Post by TrevorH » 2022/08/06 23:04:56

Learn how to write a systemd unit file and have it launch it for you. It will also restart it when it fails (if you tell it to).
The future appears to be RHEL or Debian. I think I'm going Debian.
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 and 6 are deadest, do not use them.
Use the FAQ Luke

Whoever
Posts: 1357
Joined: 2013/09/06 03:12:10

Re: Automate file start

Post by Whoever » 2022/08/07 04:36:32

jejedu67 wrote:
2022/08/06 22:47:50
Hello,

On CentoS 7 I sometimes have to launch 2 files. To do this, I run the following 4 commands:

cd /home/gms_test
./GameManagerServer&
cd /home/zs_test
./ZoneServer&

I want to know if I can automate this every 15 minutes.

Thanks in advance

Red up about cron.

jejedu67
Posts: 53
Joined: 2015/12/07 20:01:46

Re: Automate file start

Post by jejedu67 » 2022/08/07 08:01:05

Whoever wrote:
2022/08/07 04:36:32
jejedu67 wrote:
2022/08/06 22:47:50
Hello,

On CentoS 7 I sometimes have to launch 2 files. To do this, I run the following 4 commands:

cd /home/gms_test
./GameManagerServer&
cd /home/zs_test
./ZoneServer&

I want to know if I can automate this every 15 minutes.

Thanks in advance

Red up about cron.
I found how to run for example cd /home/zs_test or ./ZoneServer& with a cron job with crontab -e, but I need to do both things in one cron job...


TrevorH wrote:
2022/08/06 23:04:56
Learn how to write a systemd unit file and have it launch it for you. It will also restart it when it fails (if you tell it to).
I just need to start it each 15 minutes.

User avatar
jlehtone
Posts: 4523
Joined: 2007/12/11 08:17:33
Location: Finland

Re: Automate file start

Post by jlehtone » 2022/08/07 12:03:49

A note:

Code: Select all

cd /home/gms_test
./GameManagerServer&
cd /home/zs_test
./ZoneServer&
is same as

Code: Select all

/home/gms_test/GameManagerServer &
/home/zs_test/ZoneServer &
unless the programs expect the current working directory to be same as file location.

You can store the commands in a script file:

Code: Select all

$ cat example.sh
#!/bin/bash
/home/gms_test/GameManagerServer
/home/zs_test/ZoneServer
If you give the file executable attribute chmod +x example.sh, then you can run it:

Code: Select all

./example.sh &
You can also give the script to cron or systemd. With systemd you would have example.service unit that runs the script and example.timer unit that starts the service unit ever 15 minutes.

Are you sure that you have to run these ever 15 minutes?

User avatar
TrevorH
Site Admin
Posts: 33202
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: Automate file start

Post by TrevorH » 2022/08/07 14:08:14

Using cron is a very poor second to setting up systemd to do this for you.
The future appears to be RHEL or Debian. I think I'm going Debian.
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 and 6 are deadest, do not use them.
Use the FAQ Luke

jejedu67
Posts: 53
Joined: 2015/12/07 20:01:46

Re: Automate file start

Post by jejedu67 » 2022/08/09 05:55:28

jlehtone wrote:
2022/08/07 12:03:49
A note:

Code: Select all

cd /home/gms_test
./GameManagerServer&
cd /home/zs_test
./ZoneServer&
is same as

Code: Select all

/home/gms_test/GameManagerServer &
/home/zs_test/ZoneServer &
unless the programs expect the current working directory to be same as file location.

You can store the commands in a script file:

Code: Select all

$ cat example.sh
#!/bin/bash
/home/gms_test/GameManagerServer
/home/zs_test/ZoneServer
If you give the file executable attribute chmod +x example.sh, then you can run it:

Code: Select all

./example.sh &
You can also give the script to cron or systemd. With systemd you would have example.service unit that runs the script and example.timer unit that starts the service unit ever 15 minutes.

Are you sure that you have to run these ever 15 minutes?


Hello,

With crontab -e I add that:
Image

The chmod 754 script:
Image

Image

There is again an error ?

User avatar
jlehtone
Posts: 4523
Joined: 2007/12/11 08:17:33
Location: Finland

Re: Automate file start

Post by jlehtone » 2022/08/09 09:01:59

The cat example.sh was merely a command to show the content of file example.sh.
The content was just:

Code: Select all

#!/bin/bash
/home/gms_test/GameManagerServer
/home/zs_test/ZoneServer

Wait, you do have the script file in directory /root/scripts and you do show that directory in GUI :!:
That means that these programs are run with root privileges and you have GUI session as root.
Both are very bad practice.

jejedu67
Posts: 53
Joined: 2015/12/07 20:01:46

Re: Automate file start

Post by jejedu67 » 2022/08/09 11:09:35

I deleted cat example.sh but it still does not work.

This may be related to the missing dot, which I usually use to launch the file manually via SSH.

However, I don't really know where to put the point.

User avatar
jlehtone
Posts: 4523
Joined: 2007/12/11 08:17:33
Location: Finland

Re: Automate file start

Post by jlehtone » 2022/08/09 12:41:53

The "dot" means "current working directory". When you do
cd /home/gms_test
you set the current working directory to be /home/gms_test
In that situation ./ means /home/gms_test/

When you run a command, it inherits the current working directory (cwd) of the environment

Code: Select all

cd /tmp
/home/gms_test/GameManagerServer ( cwd==/tmp )
cd /home/gms_test
/home/gms_test/GameManagerServer ( cwd==/home/gms_test )
./GameManagerServer ( cwd==/home/gms_test )
As I said in my first comment, if the program does assume that its file is in cwd, i.e. it tries
to read other files relative to cwd, then it will not find those files.

Post Reply