T
T
timoncool2014-10-28 14:55:31
PHP
timoncool, 2014-10-28 14:55:31

I can't understand why the script clears the entire cron, the place of deleting a specific line, how to do it right?

There is a script that should remove one specific line from the cron, but for some unknown reason, it clears all the lines in the cron, I just can’t figure out what the error is.
Here is the part responsible for it

$connect = ssh2_connect('localhost', '22');
        ssh2_auth_password($connect, ssh_login, ssh_password);
        $stream = ssh2_exec($connect,'cat /var/spool/cron/crontabs/deone');
        stream_set_blocking($stream, true);
        $path_script = 'user_'.$_SESSION['id'].'_job_'.$id.'.php';
        $stream = stream_get_contents($stream);
        $stream = str_replace('* * * * * /home/jitter.sh; /usr/bin/flock -xn /tmp/'.$path_script.'.lock -c \'/usr/bin/php -q /var/www/deone/data/www/vk-manager.ru/script/grabber/'.$path_script.' >/dev/null 2>&1\'', '', $stream);
        ssh2_exec($connect,'echo -n > /var/spool/cron/crontabs/deone');
        ssh2_exec($connect, 'echo "'.$stream.'">> /var/spool/cron/crontabs/deone');
        ssh2_exec($connect, '/etc/init.d/cron restart');        
        }
    }
    $mysqli->close();

In this form, the lines in the crown are
* * * * * /home/jitter.sh; /usr/bin/flock -xn /tmp/user_751_job_11101.php.lock -c '/usr/bin/php -q /var/www/deone/data/www/vk-manager.ru/script/grabber/user_751_job_11101.php >/dev/null 2>&1'

And here is the old code, which seemed to work once correctly
$connect = ssh2_connect('localhost', '22');
        ssh2_auth_password($connect, ssh_login, ssh_password);
        $stream = ssh2_exec($connect,'cat /var/spool/cron/crontabs/deone');
        stream_set_blocking($stream, true);
        $stream = stream_get_contents($stream);
        $path_script = 'grabber/user_'.$_SESSION['id'].'_job_'.$id.'.php';
        $stream = str_replace("*	*	*	*	*	/usr/bin/php -q /var/www/deone/data/www/vk-manager.ru/script/".$path_script." >/dev/null 2>&1\n",'',$stream);
        ssh2_exec($connect,'echo -n > /var/spool/cron/crontabs/deone');
        ssh2_exec($connect, 'echo "'.$stream.'">> /var/spool/cron/crontabs/deone');
        ssh2_exec($connect, '\etc\init.d\cron restart');

Please tell me how to fix the problem. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Valery Ryaboshapko, 2014-12-20
@valerium

In general, yes, it is done wildly crooked. First, the script takes the file from the server, then processes it on its side, then transfers the entire file back. Isn't it easier to "point" process the file on the server side? There is a magic utility sed!

$connect = ssh2_connect('localhost', '22');
ssh2_auth_password($connect, ssh_login, ssh_password);
$stream = ssh2_exec($connect,"sed -i.back 's/* * * * * /home/jitter.sh; /usr/bin/flock -xn /tmp/$path_script.lock -c \'/usr/bin/php -q /var/www/deone/data/www/vk-manager.ru/script/grabber/$path_script >/dev/null 2>&1\'//' /var/spool/cron/crontabs/deone");
ssh2_exec($connect, '/etc/init.d/cron restart');

That is, the sed stream editor (a magical thing! I highly recommend learning) deletes one line in the file, while (just in case) creating a backup copy of the original file with the .back suffix.
By the way, apparently, the connection goes to the root record with a password, this is completely insecure. Can you imagine someone stealing your script? Get confused and set up sudo and key authorization.
And yes, I hope localhost is just a replacement for privacy?

A
Alexander Aksentiev, 2014-10-28
@Sanasol

Why not create one file for one job? And remember its name, then to rewrite or delete it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question