diff --git a/include/job.h b/include/job.h
index e511852..db06fc5 100644
--- a/include/job.h
+++ b/include/job.h
@@ -22,6 +22,8 @@ public:
 	Job(DBconn *conn, const wxString &jid);
 	~Job();
 
+    void ExecuteSql(wxString jstdbname,wxString jstconnstr,wxString stepid,wxString jobid,wxString code,int &rc,bool &succeeded,wxString &output);
+    void ExecuteFile(wxString stepid,wxString jobid, wxString code,int &rc,bool &succeeded,wxString &output);
 	int Execute();
 	bool Runnable()
 	{
diff --git a/job.cpp b/job.cpp
index 00334ab..84d4d28 100644
--- a/job.cpp
+++ b/job.cpp
@@ -76,11 +76,237 @@ Job::~Job()
 	LogMessage(wxString::Format(_("Completed job: %s"), jobid.c_str()), LOG_DEBUG);
 }
 
+void Job::ExecuteSql(wxString dbname,wxString connstr,wxString stepid,wxString jobid,wxString code,int &rc,bool &succeeded,wxString &output)
+{
+    DBconn *conn;
+
+	conn = DBconn::Get(connstr, dbname);
+	if (conn)
+	{
+		LogMessage(wxString::Format(_("Executing SQL step %s (part of job %s)"), stepid.c_str(), jobid.c_str()), LOG_DEBUG);
+		rc = conn->ExecuteVoid(code);
+		succeeded = conn->LastCommandOk();
+		output  += conn->GetLastError();
+		conn->Return();
+	}
+	else
+	{
+		output  += _("Couldn't get a connection to the database!");
+		succeeded = false;
+	}
+
+}
+
+void Job::ExecuteFile(wxString stepid,wxString jobid, wxString code,int &rc,bool &succeeded,wxString &output)
+{
+    // Get a temporary filename, then reuse it to create an empty directory.
+    wxString dirname = wxFileName::CreateTempFileName(wxT("pga_"));
+    if (dirname.Length() == 0)
+    {
+	    output = _("Couldn't get a temporary filename!");
+	    LogMessage(_("Couldn't get a temporary filename!"), LOG_WARNING);
+	    rc = -1;
+        return;
+    }
+
+    ;
+    if (!wxRemoveFile(dirname))
+    {
+	    output.Printf(_("Couldn't remove temporary file: %s"), dirname.c_str());
+	    LogMessage(output, LOG_WARNING);
+	    rc = -1;
+        return;
+    }
+
+    if (!wxMkdir(dirname, 0700))
+    {
+	    output.Printf(_("Couldn't create temporary directory: %s"), dirname.c_str());
+	    LogMessage(output, LOG_WARNING);
+	    rc = -1;
+        return;
+    }
+
+#ifdef __WIN32__
+	wxString filename = dirname + wxT("\\") + jobid + wxT("_") + stepid + wxT(".bat");
+    wxString errorFile = dirname + wxT("\\") + jobid + wxT("_") + stepid + wxT("_error.txt");
+#else
+	wxString filename = dirname + wxT("/") + jobid + wxT("_") + stepid + wxT(".scr");
+    wxString errorFile = dirname + wxT("/") + jobid + wxT("_") + stepid + wxT("_error.txt");
+#endif
+
+	// Write the script
+	wxFile *file = new wxFile();
+
+	if (!file->Create(filename, true, wxS_IRUSR | wxS_IWUSR | wxS_IXUSR))
+	{
+		output.Printf(_("Couldn't create temporary script file: %s"), filename.c_str());
+		LogMessage(output, LOG_WARNING);
+	    rc = -1;
+        return;
+	}
+
+	if (!file->Open(filename, wxFile::write))
+	{
+		output.Printf(_("Couldn't open temporary script file: %s"), filename.c_str());
+		LogMessage(output, LOG_WARNING);
+		wxRemoveFile(filename);
+		wxRmdir(dirname);
+		rc = -1;
+        return;
+	}
+
+	// Cleanup the code. If we're on Windows, we need to make all line ends \r\n,
+	// If we're on Unix, we need \n
+    code.Replace(wxT("\r\n"), wxT("\n"));
+#ifdef __WIN32__
+	code.Replace(wxT("\n"), wxT("\r\n"));
+#endif
+
+	if (!file->Write(code))
+	{
+		output.Printf(_("Couldn't write to temporary script file: %s"), filename.c_str());
+		LogMessage(output, LOG_WARNING);
+		wxRemoveFile(filename);
+		wxRmdir(dirname);
+		rc = -1;
+        return;
+	}
+
+	file->Close();
+	LogMessage(wxString::Format(_("Executing script file: %s"), filename.c_str()), LOG_DEBUG);
+    // freopen function is used to redirect output of stream (stderr in our case)
+	// into the specified file.
+	FILE *fpError = freopen(errorFile.mb_str(), "w", stderr);
+
+
+	// Execute the file and capture the output
+#ifdef __WIN32__
+    // The Windows way
+    HANDLE h_script, h_process;
+    DWORD dwRead;
+    char chBuf[4098];
+
+    h_script = win32_popen_r(filename.wc_str(), h_process);
+    if (!h_script)
+    {
+	    output.Printf(_("Couldn't execute script: %s, GetLastError() returned %d, errno = %d"), filename.c_str(), GetLastError(), errno);
+	    LogMessage(output, LOG_WARNING);
+	    CloseHandle(h_process);
+	    rc = -1;
+        return;
+    }
+
+
+    // Read output from the child process
+    if (h_script)
+    {
+	    for (;;)
+	    {
+		    if(!ReadFile(h_script, chBuf, 4096, &dwRead, NULL) || dwRead == 0)
+			    break;
+
+		    chBuf[dwRead] = 0;
+		    output += wxString::FromAscii(chBuf);
+	    }
+    }
+
+
+    GetExitCodeProcess(h_process, (LPDWORD)&rc);
+    CloseHandle(h_process);
+    CloseHandle(h_script);
+
+#else
+	// The *nix way.
+	FILE *fp_script;
+	char buf[4098];
+
+	fp_script = popen(filename.mb_str(wxConvUTF8), "r");
+	if (!fp_script)
+	{
+		output.Printf(_("Couldn't execute script: %s, errno = %d"), filename.c_str(), errno);
+		LogMessage(output, LOG_WARNING);
+		rc = -1;
+        return;
+	}
+
+
+	while(!feof(fp_script))
+	{
+		if (fgets(buf, 4096, fp_script) != NULL)
+			output += wxString::FromAscii(buf);
+	}
+
+	rc = pclose(fp_script);
+
+	if (WIFEXITED(rc))
+		rc = WEXITSTATUS(rc);
+	else
+		rc = -1;
+
+#endif
+    // If output is empty then either script did not return any output
+	// or script threw some error into stderr.
+	if (output == wxEmptyString)
+	{
+		// Check script threw some error into stderr
+		if (fpError)
+		{
+			fclose(fpError);
+			fpError = fopen(errorFile.mb_str(), "r");
+			if (fpError)
+			{
+				char buffer[4098];
+				wxString errorMsg = wxEmptyString;
+				while (!feof(fpError))
+				{
+					if (fgets(buffer, 4096, fpError) != NULL)
+						errorMsg += wxString(buffer, wxConvLibc);
+				}
+
+				if (errorMsg != wxEmptyString) {
+					LogMessage(wxString::Format(_("Script Error: \n%s\n"), errorMsg.c_str()), LOG_WARNING);
+					output += errorMsg.c_str();
+				}
+
+				fclose(fpError);
+			}
+			wxRemoveFile(errorFile);
+		}
+	}
+	else
+	{
+		if (fpError)
+		{
+			fclose(fpError);
+			wxRemoveFile(errorFile);
+		}
+	}
+
+
+	// Delete the file/directory. If we fail, don't overwrite the script output in the log, just throw warnings.
+	if (!wxRemoveFile(filename))
+	{
+		LogMessage(wxString::Format(_("Couldn't remove temporary script file: %s"), filename.c_str()), LOG_WARNING);
+		wxRmdir(dirname);
+        return;
+	}
+
+	if (!wxRmdir(dirname))
+	{
+		LogMessage(wxString::Format(_("Couldn't remove temporary directory: "), dirname.c_str()), LOG_WARNING);
+        return;
+	}
+
+}
+
 
 int Job::Execute()
 {
 	int rc = 0;
 	bool succeeded = false;
+    bool hasTriggerSupport = false;
+
+
 	DBresult *steps = threadConn->Execute(
 	                      wxT("SELECT * ")
 	                      wxT("  FROM pgagent.pga_jobstep ")
@@ -96,13 +322,26 @@ int Job::Execute()
 
 	while (steps->HasData())
 	{
-		DBconn *stepConn;
 		wxString jslid, stepid, jpecode, output;
 
 		stepid = steps->GetString(wxT("jstid"));
 
 		DBresult *id = threadConn->Execute(
 		                   wxT("SELECT nextval('pgagent.pga_jobsteplog_jslid_seq') AS id"));
+        
+
+        DBresult *triggerSupport = threadConn->Execute(
+		                    wxT("SELECT EXISTS(")
+                            wxT(" SELECT * ")
+                            wxT(" FROM information_schema.tables ")
+                            wxT(" WHERE ")
+                            wxT("   table_schema = 'pgagent' AND ")
+                            wxT("   table_name = 'pga_jobsteptrigger'")
+                            wxT(") AS exist;"));
+        if(triggerSupport->GetString(wxT("exist")) == wxT("t"))
+            hasTriggerSupport = true;
+
+
 		if (id)
 		{
 			jslid = id->GetString(wxT("id"));
@@ -131,25 +370,11 @@ int Job::Execute()
 		{
 			case 's':
 			{
+
 				wxString jstdbname = steps->GetString(wxT("jstdbname"));
 				wxString jstconnstr = steps->GetString(wxT("jstconnstr"));
 
-				stepConn = DBconn::Get(jstconnstr, jstdbname);
-				if (stepConn)
-				{
-					LogMessage(wxString::Format(_("Executing SQL step %s (part of job %s)"), stepid.c_str(), jobid.c_str()), LOG_DEBUG);
-					rc = stepConn->ExecuteVoid(steps->GetString(wxT("jstcode")));
-					succeeded = stepConn->LastCommandOk();
-					output = stepConn->GetLastError();
-					stepConn->Return();
-				}
-				else
-				{
-					output = _("Couldn't get a connection to the database!");
-					succeeded = false;
-				}
-
-
+                ExecuteSql(jstdbname,jstconnstr,stepid,jobid,steps->GetString(wxT("jstcode")),rc,succeeded,output);
 				break;
 			}
 			case 'b':
@@ -157,166 +382,14 @@ int Job::Execute()
 				// Batch jobs are more complex thank SQL, for obvious reasons...
 				LogMessage(wxString::Format(_("Executing batch step %s (part of job %s)"), stepid.c_str(), jobid.c_str()), LOG_DEBUG);
 
-				// Get a temporary filename, then reuse it to create an empty directory.
-				wxString dirname = wxFileName::CreateTempFileName(wxT("pga_"));
-				if (dirname.Length() == 0)
-				{
-					output = _("Couldn't get a temporary filename!");
-					LogMessage(_("Couldn't get a temporary filename!"), LOG_WARNING);
-					rc = -1;
-					break;
-				}
-
-				;
-				if (!wxRemoveFile(dirname))
-				{
-					output.Printf(_("Couldn't remove temporary file: %s"), dirname.c_str());
-					LogMessage(output, LOG_WARNING);
-					rc = -1;
-					break;
-				}
-
-				if (!wxMkdir(dirname, 0700))
-				{
-					output.Printf(_("Couldn't create temporary directory: %s"), dirname.c_str());
-					LogMessage(output, LOG_WARNING);
-					rc = -1;
-					break;
-				}
-
-#ifdef __WIN32__
-				wxString filename = dirname + wxT("\\") + jobid + wxT("_") + stepid + wxT(".bat");
-#else
-				wxString filename = dirname + wxT("/") + jobid + wxT("_") + stepid + wxT(".scr");
-#endif
-
-				// Write the script
-				wxFile *file = new wxFile();
-
-				if (!file->Create(filename, true, wxS_IRUSR | wxS_IWUSR | wxS_IXUSR))
-				{
-					output.Printf(_("Couldn't create temporary script file: %s"), filename.c_str());
-					LogMessage(output, LOG_WARNING);
-					rc = -1;
-					break;
-				}
-
-				if (!file->Open(filename, wxFile::write))
-				{
-					output.Printf(_("Couldn't open temporary script file: %s"), filename.c_str());
-					LogMessage(output, LOG_WARNING);
-					wxRemoveFile(filename);
-					wxRmdir(dirname);
-					rc = -1;
-					break;
-				}
-
-				wxString code = steps->GetString(wxT("jstcode"));
-
-				// Cleanup the code. If we're on Windows, we need to make all line ends \r\n,
-				// If we're on Unix, we need \n
-				code.Replace(wxT("\r\n"), wxT("\n"));
-#ifdef __WIN32__
-				code.Replace(wxT("\n"), wxT("\r\n"));
-#endif
-
-				if (!file->Write(code))
-				{
-					output.Printf(_("Couldn't write to temporary script file: %s"), filename.c_str());
-					LogMessage(output, LOG_WARNING);
-					wxRemoveFile(filename);
-					wxRmdir(dirname);
-					rc = -1;
-					break;
-				}
-
-				file->Close();
-				LogMessage(wxString::Format(_("Executing script file: %s"), filename.c_str()), LOG_DEBUG);
-
-				// Execute the file and capture the output
-#ifdef __WIN32__
-				// The Windows way
-				HANDLE h_script, h_process;
-				DWORD dwRead;
-				char chBuf[4098];
-
-				h_script = win32_popen_r(filename.wc_str(), h_process);
-				if (!h_script)
-				{
-					output.Printf(_("Couldn't execute script: %s, GetLastError() returned %d, errno = %d"), filename.c_str(), GetLastError(), errno);
-					LogMessage(output, LOG_WARNING);
-					CloseHandle(h_process);
-					rc = -1;
-					break;
-				}
-
-
-				// Read output from the child process
-				if (h_script)
-				{
-					for (;;)
-					{
-						if(!ReadFile(h_script, chBuf, 4096, &dwRead, NULL) || dwRead == 0)
-							break;
-
-						chBuf[dwRead] = 0;
-						output += wxString::FromAscii(chBuf);
-					}
-				}
-
-
-				GetExitCodeProcess(h_process, (LPDWORD)&rc);
-				CloseHandle(h_process);
-				CloseHandle(h_script);
-
-#else
-				// The *nix way.
-				FILE *fp_script;
-				char buf[4098];
-
-				fp_script = popen(filename.mb_str(wxConvUTF8), "r");
-				if (!fp_script)
-				{
-					output.Printf(_("Couldn't execute script: %s, errno = %d"), filename.c_str(), errno);
-					LogMessage(output, LOG_WARNING);
-					rc = -1;
-					break;
-				}
-
-
-				while(!feof(fp_script))
-				{
-					if (fgets(buf, 4096, fp_script) != NULL)
-						output += wxString::FromAscii(buf);
-				}
-
-				rc = pclose(fp_script);
-
-				if (WIFEXITED(rc))
-					rc = WEXITSTATUS(rc);
-				else
-					rc = -1;
-
-#endif
-
+                
+                ExecuteFile(stepid,jobid,steps->GetString(wxT("jstcode")),rc,succeeded,output);
 				// set success status for batch runs, be pessimistic by default
 				LogMessage(wxString::Format(_("Script return code: %d"), rc), LOG_DEBUG);
 				if (rc == 0)
 					succeeded = true;
 
-				// Delete the file/directory. If we fail, don't overwrite the script output in the log, just throw warnings.
-				if (!wxRemoveFile(filename))
-				{
-					LogMessage(wxString::Format(_("Couldn't remove temporary script file: %s"), filename.c_str()), LOG_WARNING);
-					wxRmdir(dirname);
-					break;
-				}
-
-				if (!wxRmdir(dirname))
-				{
-					LogMessage(wxString::Format(_("Couldn't remove temporary directory: "), dirname.c_str()), LOG_WARNING);
-					break;
-				}
+                
 
 				break;
 			}
@@ -328,23 +401,89 @@ int Job::Execute()
 			}
 		}
 
+
 		wxString stepstatus;
-		if (succeeded)
+		wxString triggerstatus;
+		if (succeeded) {
 			stepstatus = wxT("s");
+			triggerstatus = wxT("s");
+        }
 		else
+        {
 			stepstatus = steps->GetString(wxT("jstonerror"));
+			triggerstatus = wxT("f");
+        }
+
+
 
-		rc = threadConn->ExecuteVoid(
+        rc = threadConn->ExecuteVoid(
 		         wxT("UPDATE pgagent.pga_jobsteplog ")
 		         wxT("   SET jslduration = now() - jslstart, ")
-		         wxT("       jslresult = ") + NumToStr(rc) + wxT(", jslstatus = '") + stepstatus + wxT("', ")
+		         wxT("       jslresult = ") + NumToStr(rc) + wxT(", jslstatus = '") + stepstatus + wxT("' ")
+		         wxT(" WHERE jslid=") + jslid);
+
+
+        if(hasTriggerSupport)
+        {
+            DBresult *trigger = threadConn->Execute(
+		                       wxT("SELECT * FROM pgagent.pga_jobsteptrigger WHERE jsrjstid = ") + stepid);
+            if (triggerstatus == trigger->GetString(wxT("jsronerror")))
+            {
+                output += _("\nTrigger Job :\n");
+                LogMessage(_("Triggered"), LOG_WARNING);
+
+
+                switch ((int) trigger->GetString(wxT("jsrkind"))[0])
+		        {
+			        case 's':
+			        {
+				        wxString jsrdbname = trigger->GetString(wxT("jsrdbname"));
+				        wxString jsrconnstr = trigger->GetString(wxT("jsrconnstr"));
+
+				        ExecuteSql(jsrdbname,jsrconnstr,stepid,jobid,trigger->GetString(wxT("jsrcode")),rc,succeeded,output);
+
+				        break;
+			        }
+			        case 'b':
+			        {
+                        LogMessage(wxString::Format(_("Executing trigger batch step %s (part of job %s)"), stepid.c_str(), jobid.c_str()), LOG_DEBUG);
+
+                    
+                        ExecuteFile(stepid,jobid,trigger->GetString(wxT("jsrcode")),rc,succeeded,output);
+
+			            // set success status for batch runs, be pessimistic by default
+			            LogMessage(wxString::Format(_("Script return code: %d"), rc), LOG_DEBUG);
+				        if (rc == 0)
+					        succeeded = true;
+				        break;
+			        }
+			        default:
+			        {
+				        output = _("Invalid trigger step type!");
+				        return -1;
+			        }
+
+		        }
+
+                
+            }
+
+            if(succeeded)
+                output += _("Succeeded");
+        }
+        rc = threadConn->ExecuteVoid(
+		         wxT("UPDATE pgagent.pga_jobsteplog ")
+		         wxT("   SET ")
 		         wxT("       jsloutput = ") + threadConn->qtDbString(output) + wxT(" ")
 		         wxT(" WHERE jslid=") + jslid);
+
 		if (rc != 1 || stepstatus == wxT("f"))
 		{
 			status = wxT("f");
 			return -1;
 		}
+
+		
 		steps->MoveNext();
 	}
 	delete steps;
diff --git a/sql/pgagent--unpackaged--3.4.sql b/sql/pgagent--unpackaged--3.4.sql
index 536640e..7896ee6 100644
--- a/sql/pgagent--unpackaged--3.4.sql
+++ b/sql/pgagent--unpackaged--3.4.sql
@@ -18,6 +18,7 @@ ALTER EXTENSION pgagent ADD TABLE pgagent.pga_schedule;
 ALTER EXTENSION pgagent ADD TABLE pgagent.pga_exception;
 ALTER EXTENSION pgagent ADD TABLE pgagent.pga_joblog;
 ALTER EXTENSION pgagent ADD TABLE pgagent.pga_jobsteplog;
+ALTER EXTENSION pgagent ADD TABLE pgagent.pga_jobsteptrigger;
 
 ALTER EXTENSION pgagent ADD SEQUENCE pgagent.pga_exception_jexid_seq;
 ALTER EXTENSION pgagent ADD SEQUENCE pgagent.pga_job_jobid_seq;
@@ -26,6 +27,7 @@ ALTER EXTENSION pgagent ADD SEQUENCE pgagent.pga_joblog_jlgid_seq;
 ALTER EXTENSION pgagent ADD SEQUENCE pgagent.pga_jobstep_jstid_seq;
 ALTER EXTENSION pgagent ADD SEQUENCE pgagent.pga_jobsteplog_jslid_seq;
 ALTER EXTENSION pgagent ADD SEQUENCE pgagent.pga_schedule_jscid_seq;
+ALTER EXTENSION pgagent ADD SEQUENCE pgagent.pga_jobsteptrigger_jsrid_seq;
 
 ALTER EXTENSION pgagent ADD FUNCTION pgagent.pgagent_schema_version();
 ALTER EXTENSION pgagent ADD FUNCTION pgagent.pga_next_schedule(int4, timestamptz, timestamptz, _bool, _bool, _bool, _bool, _bool);
@@ -42,3 +44,4 @@ SELECT pg_catalog.pg_extension_config_dump('pga_schedule', '');
 SELECT pg_catalog.pg_extension_config_dump('pga_exception', '');
 SELECT pg_catalog.pg_extension_config_dump('pga_joblog', '');
 SELECT pg_catalog.pg_extension_config_dump('pga_jobsteplog', '');
+SELECT pg_catalog.pg_extension_config_dump('pga_jobsteptrigger', '');
diff --git a/sql/pgagent.sql b/sql/pgagent.sql
index 3708adf..8b30d09 100644
--- a/sql/pgagent.sql
+++ b/sql/pgagent.sql
@@ -80,6 +80,30 @@ COMMENT ON COLUMN pgagent.pga_jobstep.jstonerror IS 'What to do if step returns
 
 
 
+CREATE TABLE pgagent.pga_jobsteptrigger (
+jsrid SERIAL,
+jsrjstid INTEGER NOT NULL,
+jsrkind CHAR(1) NOT NULL,
+jsrcode TEXT NOT NULL,
+jsrconnstr TEXT DEFAULT ''::text NOT NULL,
+jsrdbname NAME DEFAULT ''::name NOT NULL,
+jsronerror CHAR(1) DEFAULT 'f'::bpchar NOT NULL,
+jsrenabled BOOLEAN DEFAULT false NOT NULL,
+CONSTRAINT pga_jobsteptrigger_pkey PRIMARY KEY(jsrid),
+CONSTRAINT pga_jobsteptrigger_chk CHECK (jsrkind = ANY (ARRAY['b'::bpchar, 's'::bpchar])),
+CONSTRAINT pga_jobsteptrigger_chk1 CHECK (((jsrconnstr <> ''::text) AND (jsrkind = 's'::bpchar)) OR ((jsrconnstr = ''::text) AND ((jsrkind = 'b'::bpchar) OR (jsrdbname <> ''::name)))),
+CONSTRAINT pga_jobsteptrigger_chk2 CHECK (((jsrdbname <> ''::name) AND (jsrkind = 's'::bpchar)) OR ((jsrdbname = ''::name) AND ((jsrkind = 'b'::bpchar) OR (jsrconnstr <> ''::text)))),
+CONSTRAINT pga_jobsteptrigger_chk3 CHECK (jsronerror = ANY (ARRAY['f'::bpchar, 's'::bpchar])),
+CONSTRAINT pga_jobsteptrigger_fk FOREIGN KEY (jsrjstid)
+REFERENCES pgagent.pga_jobstep(jstid) ON DELETE CASCADE ON UPDATE CASCADE NOT DEFERRABLE
+)  WITHOUT OIDS;
+COMMENT ON TABLE pgagent.pga_jobsteptrigger IS 'Job step trigger to be executed';
+COMMENT ON COLUMN pgagent.pga_jobsteptrigger.jsrkind IS 'Kind of jobstep: s=sql, b=batch';
+COMMENT ON COLUMN pgagent.pga_jobsteptrigger.jsronerror IS 'f: fail s:success';
+CREATE INDEX pga_jobsteptrigger_jsrjstid_idx ON pgagent.pga_jobsteptrigger USING btree (jsrjstid);
+
+
+
 CREATE TABLE pgagent.pga_schedule (
 jscid                serial               NOT NULL PRIMARY KEY,
 jscjobid             int4                 NOT NULL REFERENCES pgagent.pga_job (jobid) ON DELETE CASCADE ON UPDATE RESTRICT,
