Crontab command – working status, log, cron job

When setup crontab on the server, it’s hard to check whether it’s working properly or not.

There’s command to check the status and log.

To check cron status and see the latest cronjob:

/etc/init.d/cron status

To start running:

service cron start

To stop running:

service cron stop

To restart running:

service cron restart

Cronjob example:

Running php script every minute:

* * * * * /usr/bin/php -e /websites/example.php > /dev/null 2>&1

/dev/null  this line is added for no log

If you want to see running log:

* * * * * /usr/bin/php -e /websites/example.php > /home/user/cron.log 2>&1

/dev/null  this line is added for saving to log file

Running php script every 2 minutes, which will run 30 times per hour:

*/2 * * * * /usr/bin/php -e /websites/example.php > /dev/null 2>&1

Running php script every 10 minutes, which will run 6 times per hour:

*/10 * * * * /usr/bin/php -e /websites/example.php > /dev/null 2>&1

Running php script designated minutes, this will running every 1,11,21,31,41,51 minutes:

1,11,21,31,41,51 * * * * /usr/bin/php -e /websites/example.php > /dev/null 2>&1

Running php script once a day, this will running at 1:00am every day:

0 1 * * * /usr/bin/php -e /websites/example.php > /dev/null 2>&1

Let’s keep the time well.

Mysql error 1364 Field doesn’t have a default values

When get this error: mysql error 1364 Field doesn’t have a default values.

This happens when there’s key index but no values given in STRICT_TRANS_TABLES mode.

So we need to change sql_mode which is defined in my.cnf file.

When I search about the file, unfortunately I couldn’t find the file.

So rather than modify file, use query to change mysql configuration:

SET GLOBAL sql_mode = 'ONLY_FULL_GROUP_BY,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'

If this doesn’t work or it doesn’t allow you to query, you can modify mysql.cnf file.

vi /etc/mysql/conf.d/mysql.cnf

Then add these line

[mysqld] sql_mode = "ONLY_FULL_GROUP_BY,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

And reboot mysql.

sudo service mysql restart

If still not working, add this disable_strict_mode.cnf file below location.

vi  /etc/mysql/conf.d/disable_strict_mode.cnf

Then add these 2 lines.

sql_mode = ONLY_FULL_GROUP_BY,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

After reboot, check it by running this query:

SHOW VARIABLES LIKE 'sql_mode';

Login to server using SSH key, without password

When login into server using SSH,  it’s more safe and easier using SSH key rather than password.

Before start, check my username on the computer
whoami
And use this username for easier setup

1. On remote server, switch to sudor

sudo su

2. Adding user and put password

adduser {user}

3. Switch to the created user

su {user}
cd ~/.ssh

4. Create key

ssh-keygen

5. Open ~/.ssh/id_rsa.pub from local computer and copy public key

ssh-rsa {some keys goes here}

6. Create authorized_keys file on remote server and paste public key

vi authorized_keys

7. Change Authorized key permission, make sure it has 600 permission

chmod 600 authorized_keys

8. Optionally, delete id_rsa, id_rsa.pub on remove server, it’s not used and not necessary to keep.

9. make login without password

sudo vi /etc/ssh/sshd_config

make sure PermitRootLogin line has like this:

PermitRootLogin without-password

10. Reload ssh

sudo service ssh restart

11. Try to login to remote server without password:

ssh {user}@{remove-server-ip}

12. Optionally, you can login without user name, just use ip address:
sudo su

vi /etc/sudoers.d/admin

Add like this

{user} ALL=(ALL) NOPASSWD:ALL

After change, give admin file permission to 400.

chmod 400 admin

13. If you want to allow login with password SSH or SFTP, change below.

sudo vi /etc/ssh/sshd_config

Then uncomment below

PasswordAuthentication yes

Delete git repository histories

One of my site git file became too big, and took long time to clone.
So I had to clean it.

1. Checkout

git checkout --orphan latest_branch

2. Add all the files

git add -A

3. Commit the changes

git commit -am "commit message"

4. Delete the branch

git branch -D master

5. Rename the current branch to master

git branch -m master

6. Finally, force update your repository

git push -f origin master

To be a good house keeper.

Upgrade PHP5 to PHP7

I have older server to upgrade to PHP7.

PHP7 has better performance and cleaner library.
But some of functions are depricated like ereg_replace, ereg and also mysql is not supported anymore. Instead, preg_replace, preg_match and mysqli are available.

So some of php programming may not work and need to be updated.

To upgrade, use below commands:

sudo add-apt-repository ppa:ondrej/php
sudo apt-get install php7.0 libapache2-mod-php7.0
sudo a2dismod php5
sudo a2enmod php7.0
sudo apachectl restart

It’s quite simple, but I had to change those depricated functions.
Especially mysqli’s usage is slightly different than mysql.

To connect db:

$this->lid = mysqli_connect(DB_HOST,DB_USER,DB_PWD,DB_NAME);

Select DB:

mysqli_select_db($this->lid,DB_NAME)

Query:

$this->qid = mysqli_query($this->lid, $q);

Fetch Array:

$this->record = mysqli_fetch_array($this->qid);

Upgrading to PHP7 wasn’t night mare, it was quite simple and easy.

Installing Memcached PHP extension on CentOS 7

I have new server required to use PHP memcached.

But it showed this error.

PHP Fatal error: Class 'Memcached' not found

The server version is CentOS 7, so used below commands.

Check linux version: lsb_release -a
yum clean all
yum -y update
yum -y install memcached
systemctl start memcached.service
systemctl enable memcached.service
systemctl restart memcached.service

Run this to check memcached installed:

php -m | grep memcache

You should see like “memcache”, if it is installed correctly.

Restart apache:

systemctl restart httpd