Program który sprawdza czy porces jest odpalony.

0

Witam, mam mały problem.
Mianowicie chciałbym zapytać czy dałoby radę napisać program który sprawdzałby czy proces "A" jest odpalony i jeśli nie to odpalał by go. Potrzebuje go aby gdy nie mam dostępu do servera dedykowanego przy jakiejkolwiek przyczynie wyłączenia się procesu "A" odpalał go z powrotem.

0

c czy c++ to troche duzo jak na cos takiego, proponuje wrzucic do crona skrypt:

#!/bin/bash

#while /bin/true ; do

nazwaprocesu='wypelnij'
[ $(pgrep "$nazwaprocesu" | wc -l) == 0 ] &&  /sciezka/do/programu

#sleep 300
#done

while/sleep/done jakbys chcial pominac crona

0

Chodziło mi o coś mniej konsolowego :P Jeśli to za mało na C/C++ to może w Delphi ? :P Chodzi mi tylko o ten kod główny bez oprawy graficznej (dam radę ją sam zrobić :P).

0

Chodzi o Windows ?
Tu masz przykład jak uzyskać listę procesów w Delphi (funkjca ListaPlikow):
Wyłączanie procesów
Analogicznie zrobisz to w C/C++, po prostu trzeba skorzystać z WinAPI (CreateToolHelp32SnapShot, Process32First, Process32Next).

0

Mam coś takiego:
Mógłby mi ktoś wyjaśnic co jest tutaj nie tak?

[code]unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, TlHelp32, ShellApi, ExtCtrls, StdCtrls;

type
TForm1 = class(TForm)
Timer1: TTimer;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
Button1: TButton;
procedure Timer1Timer(Sender: TObject);
[color=purple] procedure Button1Click(Sender: TObject);[/color]
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

function processExists(exeFileName: string): Boolean;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
Result := False;
while Integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then
begin
Result := True;
end;
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
Zmienna1, Zmienna2 : String;
[color=purple]procedure TForm1.Button1Click(Sender: TObject);[/color]
begin
Zmienna1 := Edit1.Text;
Zmienna2 := Edit2.Text;
if not processExists('Zmienna1') then
ShellExecute(0, 'open', 'Zmienna2', nil, nil, SW_NORMAL);
end;

end;

end.
[/code]

Do tych fioletowych się przyczepia się przyczepia :

quote: ';' expected but '.' found
(18): Unsatisfied forward or external declaration: 'TForm1.Button1Click'[/quote]

Zmieniłem '.' na ';'

Ale ciągle wyskakuje błąd
[quote]Unknowkdirective: 'Button1Click'
Declaration expected byt '(' found
Unsatisfied forward or external declaration: 'TForm1.Button1Click'[/quote]

0

Hmm, ty miałeś kiedyś z Delphi do czynienia ?
Trzymaj kod w C++:

#include <windows.h>
#include <tlhelp32.h>

bool ProcessExists(const char *exeFileName)
{
	HANDLE hSnapshot;
	PROCESSENTRY32 processEntry = { sizeof(processEntry) };

#if defined(UNICODE)
	LPTSTR szExe = new TCHAR[strlen(exeFileName) + 1];
	wsprintf(szExe, TEXT("%hs"), exeFileName);
#else
	LPCTSTR szExe = exeFileName;
#endif

	hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	BOOL result = Process32First(hSnapshot, &processEntry);
	while (result && (lstrcmpi(szExe, processEntry.szExeFile) != 0))
	{
		result = Process32Next(hSnapshot, &processEntry);
	}
	CloseHandle(hSnapshot);

#if defined(UNICODE)
	delete szExe;
#endif

	return result;
}
0

próbuje to skompilować w Dev-C++ ale wyskakuje mi błąd.

Compiler: Default compiler
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Documents and Settings\SysOp\My Documents\Untitled2.cpp" -o "C:\Documents and Settings\SysOp\My Documents\Untitled2.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:\Dev-Cpp\lib/libmingw32.a(main.o)(.text+0x106):main.c: undefined reference to `WinMain@16'
collect2: ld returned 1 exit status

Execution terminated

Z C++ w ogóle styczności nie miałem

0
Dd4b3x napisał(a)

próbuje to skompilować w Dev-C++ ale wyskakuje mi błąd.

  1. Dev-C++ to zło.
  2. No wiesz, to jest tylko fragment kodu, takie rzeczy jak main() (czy tam WinMain()) musisz se (ew. sobie) sam napisać.

1 użytkowników online, w tym zalogowanych: 0, gości: 1