Z
Z
Zaur Ashurbekov2017-10-04 21:30:54
PostgreSQL
Zaur Ashurbekov, 2017-10-04 21:30:54

How to programmatically manage processes in Linux?

Hello Toaster!
What interests me is this: you need to start processes programmatically in the console, save and get their id (id probably?), And then, if necessary, stop them exactly by their id.
Something like:

id = system("rails s") // запуск сервера
....
if has_process(id)
    stop_process(id)
end

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaliy, 2019-07-02
@kiukishenkaec

As a result, the request turned out like this

select "channels".*, 
(select "start" from "channel_program" where "channel_id" = "channels"."id" and "end" < '2019-07-02 06:10:21' order by "start" desc limit 1) as "start_time_show", 
(select "start" from "channel_program" where "channel_id" = "channels"."id" and "start" <= '2019-07-02 06:10:21' and "end" > '2019-07-02 06:10:21' limit 1) as "start_time_current", 
(select "start" from "channel_program" where "channel_id" = "channels"."id" and "start" > '2019-07-02 06:10:21' limit 1) as "start_time_notShow"
    from "channels" where "channels"."deleted_at" is null

Made through scope
public function scopeScheduleShows($query)
    {
        $date = \Carbon\Carbon::now();
        $query->addSubSelect('start_time_show', ChannelProgram::select('start')
            ->whereColumn('channel_id', 'channels.id')
            ->where('end','<', $date)
            ->latest('start')
        )->with('show.program');
    }
    public function scopeScheduleCurrents($query)
    {
        $date = \Carbon\Carbon::now();
        $query->addSubSelect('start_time_current', ChannelProgram::select('start')
            ->whereColumn('channel_id', 'channels.id')
            ->where('start','<=', $date)
            ->where('end','>', $date)
        )->with('current.program');
    }

    public function scopeScheduleNotShows($query)
    {
        $date = \Carbon\Carbon::now();
        $query->addSubSelect('start_time_notShow', ChannelProgram::select('start')
            ->whereColumn('channel_id', 'channels.id')
            ->where('start','>', $date)
        )->with('notShow.program');
    }

In the controller I call like this
This article helped to create subqueries https://laravelnews.ru/dinamicheskie-otnosheniya-v... It describes where "addSubSelect" comes from.

M
Maxim Y, 2019-06-27
@x_shader

Since the tag is not an ORM, you can run your SQL test data.
Substitute your $date fortimestamp '2019-06-27 11:15:00'

/*
with channel_program as (
  select 1 as channel_id, 1 as program_id, timestamp '2019-06-27 09:00:00' as start_time, timestamp '2019-06-27 10:00:00' as end_time union all
  select 1 as channel_id, 2 as program_id, timestamp '2019-06-27 10:02:00' as start_time, timestamp '2019-06-27 11:00:00' as end_time union all
  select 1 as channel_id, 3 as program_id, timestamp '2019-06-27 11:10:00' as start_time, timestamp '2019-06-27 11:30:00' as end_time union all
  select 1 as channel_id, 4 as program_id, timestamp '2019-06-27 11:35:00' as start_time, timestamp '2019-06-27 12:00:00' as end_time union all
  select 1 as channel_id, 4 as program_id, timestamp '2019-06-27 12:00:00' as start_time, timestamp '2019-06-27 12:30:00' as end_time
)
*/

select t.program_id
  from (
        select 
               program_id
              ,lag(start_time) over (order by start_time) lag_start_time
              ,lead(end_time) over (order by start_time) lead_end_time
          from channel_program
       ) t
 where timestamp '2019-06-27 11:15:00' between coalesce(t.lag_start_time, timestamp '1991-01-01 00:00:00') 
                                           and coalesce(t.lead_end_time, timestamp '2999-01-01 00:00:00')

S
Stanislav Vlasov, 2017-11-15
@Stanislavvv

I strongly recommend either to put the question narrower (what kind of process should be monitored and how it is supposed to be stopped), or to read something more in-depth about Unix in general, Linux in particular and bash for writing scripts.
In the current problem statement, you will need 1) knowledge of what a process is, its PID, where to get it from, how to manage processes in Linux, 2) knowledge of how a particular server starts and where it writes its PID when it is daemonized, 3) the ability to write scripts at least on the bash ("console", damn it!).
In the meantime, you’ll understand what exactly you need, why it’s impossible to advise anything.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question